Search in sources :

Example 6 with UtteranceProgressDispatcher

use of android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher in project android_frameworks_base by ParanoidAndroid.

the class AudioPlaybackQueueItem method run.

@Override
public void run() {
    final UtteranceProgressDispatcher dispatcher = getDispatcher();
    dispatcher.dispatchOnStart();
    mPlayer = MediaPlayer.create(mContext, mUri);
    if (mPlayer == null) {
        dispatcher.dispatchOnError();
        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();
            }
        });
        mPlayer.setAudioStreamType(mStreamType);
        mPlayer.start();
        mDone.block();
        finish();
    } catch (IllegalArgumentException ex) {
        Log.w(TAG, "MediaPlayer failed", ex);
        mDone.open();
    }
    if (mFinished) {
        dispatcher.dispatchOnDone();
    } else {
        dispatcher.dispatchOnError();
    }
}
Also used : UtteranceProgressDispatcher(android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher) MediaPlayer(android.media.MediaPlayer)

Example 7 with UtteranceProgressDispatcher

use of android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher in project platform_frameworks_base by android.

the class SynthesisPlaybackQueueItem method run.

@Override
public void run() {
    final UtteranceProgressDispatcher dispatcher = getDispatcher();
    dispatcher.dispatchOnStart();
    if (!mAudioTrack.init()) {
        dispatcher.dispatchOnError(TextToSpeech.ERROR_OUTPUT);
        return;
    }
    try {
        byte[] buffer = null;
        // OR (c) done() is called in which case it will return null.
        while ((buffer = take()) != null) {
            mAudioTrack.write(buffer);
            mLogger.onAudioDataWritten();
        }
    } catch (InterruptedException ie) {
        if (DBG)
            Log.d(TAG, "Interrupted waiting for buffers, cleaning up.");
    }
    mAudioTrack.waitAndRelease();
    if (mStatusCode == TextToSpeech.SUCCESS) {
        dispatcher.dispatchOnSuccess();
    } else if (mStatusCode == TextToSpeech.STOPPED) {
        dispatcher.dispatchOnStop();
    } else {
        dispatcher.dispatchOnError(mStatusCode);
    }
    mLogger.onCompleted(mStatusCode);
}
Also used : UtteranceProgressDispatcher(android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher)

Example 8 with UtteranceProgressDispatcher

use of android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher in project platform_frameworks_base by android.

the class AudioPlaybackQueueItem method run.

@Override
public void run() {
    final UtteranceProgressDispatcher dispatcher = getDispatcher();
    dispatcher.dispatchOnStart();
    int sessionId = mAudioParams.mSessionId;
    mPlayer = MediaPlayer.create(mContext, mUri, null, mAudioParams.mAudioAttributes, sessionId > 0 ? sessionId : AudioManager.AUDIO_SESSION_ID_GENERATE);
    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();
    }
}
Also used : UtteranceProgressDispatcher(android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher) MediaPlayer(android.media.MediaPlayer)

Example 9 with UtteranceProgressDispatcher

use of android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher in project android_frameworks_base by AOSPA.

the class SynthesisPlaybackQueueItem method run.

@Override
public void run() {
    final UtteranceProgressDispatcher dispatcher = getDispatcher();
    dispatcher.dispatchOnStart();
    if (!mAudioTrack.init()) {
        dispatcher.dispatchOnError(TextToSpeech.ERROR_OUTPUT);
        return;
    }
    try {
        byte[] buffer = null;
        // OR (c) done() is called in which case it will return null.
        while ((buffer = take()) != null) {
            mAudioTrack.write(buffer);
            mLogger.onAudioDataWritten();
        }
    } catch (InterruptedException ie) {
        if (DBG)
            Log.d(TAG, "Interrupted waiting for buffers, cleaning up.");
    }
    mAudioTrack.waitAndRelease();
    if (mStatusCode == TextToSpeech.SUCCESS) {
        dispatcher.dispatchOnSuccess();
    } else if (mStatusCode == TextToSpeech.STOPPED) {
        dispatcher.dispatchOnStop();
    } else {
        dispatcher.dispatchOnError(mStatusCode);
    }
    mLogger.onCompleted(mStatusCode);
}
Also used : UtteranceProgressDispatcher(android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher)

Example 10 with UtteranceProgressDispatcher

use of android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher in project android_frameworks_base by DirtyUnicorns.

the class AudioPlaybackQueueItem method run.

@Override
public void run() {
    final UtteranceProgressDispatcher dispatcher = getDispatcher();
    dispatcher.dispatchOnStart();
    int sessionId = mAudioParams.mSessionId;
    mPlayer = MediaPlayer.create(mContext, mUri, null, mAudioParams.mAudioAttributes, sessionId > 0 ? sessionId : AudioManager.AUDIO_SESSION_ID_GENERATE);
    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();
    }
}
Also used : UtteranceProgressDispatcher(android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher) MediaPlayer(android.media.MediaPlayer)

Aggregations

UtteranceProgressDispatcher (android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher)12 MediaPlayer (android.media.MediaPlayer)6