FileDocCategorySizeDatePackage
AudioPlaybackQueueItem.javaAPI DocAndroid 5.1 API4235Thu Mar 12 22:22:10 GMT 2015android.speech.tts

AudioPlaybackQueueItem

public class AudioPlaybackQueueItem extends PlaybackQueueItem

Fields Summary
private static final String
TAG
private final android.content.Context
mContext
private final android.net.Uri
mUri
private final android.speech.tts.TextToSpeechService.AudioOutputParams
mAudioParams
private final android.os.ConditionVariable
mDone
private android.media.MediaPlayer
mPlayer
private volatile boolean
mFinished
Constructors Summary
AudioPlaybackQueueItem(android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher dispatcher, Object callerIdentity, android.content.Context context, android.net.Uri uri, android.speech.tts.TextToSpeechService.AudioOutputParams audioParams)


     
             
                  
        super(dispatcher, callerIdentity);

        mContext = context;
        mUri = uri;
        mAudioParams = audioParams;

        mDone = new ConditionVariable();
        mPlayer = null;
        mFinished = false;
    
Methods Summary
private static final floatclip(float value, float min, float max)

        return value < min ? min : (value < max ? value : max);
    
private voidfinish()

        try {
            mPlayer.stop();
        } catch (IllegalStateException ex) {
            // Do nothing, the player is already stopped
        }
        mPlayer.release();
    
public voidrun()

        final UtteranceProgressDispatcher dispatcher = getDispatcher();

        dispatcher.dispatchOnStart();

        int sessionId = mAudioParams.mSessionId;
        mPlayer = MediaPlayer.create(
                mContext, mUri, null, mAudioParams.mAudioAttributes,
                sessionId > 0 ? sessionId : AudioSystem.AUDIO_SESSION_ALLOCATE);
        if (mPlayer == null) {
            dispatcher.dispatchOnError(TextToSpeech.ERROR_OUTPUT);
            return;
        }

        try {
            mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Log.w(TAG, "Audio playback error: " + what + ", " + extra);
                    mDone.open();
                    return true;
                }
            });
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mFinished = true;
                    mDone.open();
                }
            });

            setupVolume(mPlayer, mAudioParams.mVolume, mAudioParams.mPan);
            mPlayer.start();
            mDone.block();
            finish();
        } catch (IllegalArgumentException ex) {
            Log.w(TAG, "MediaPlayer failed", ex);
            mDone.open();
        }

        if (mFinished) {
            dispatcher.dispatchOnSuccess();
        } else {
            dispatcher.dispatchOnStop();
        }
    
private static voidsetupVolume(android.media.MediaPlayer player, float volume, float pan)

        final float vol = clip(volume, 0.0f, 1.0f);
        final float panning = clip(pan, -1.0f, 1.0f);

        float volLeft = vol, volRight = vol;
        if (panning > 0.0f) {
            volLeft *= (1.0f - panning);
        } else if (panning < 0.0f) {
            volRight *= (1.0f + panning);
        }
        player.setVolume(volLeft, volRight);
    
voidstop(int errorCode)

        mDone.open();