@@ -80,27 +80,23 @@ class PlaybackSynthesisCallback extends AbstractSynthesisCallback {
8080
8181 @ Override
8282 void stop () {
83+ stopImpl (false );
84+ }
85+
86+ void stopImpl (boolean wasError ) {
8387 if (DBG ) Log .d (TAG , "stop()" );
8488
8589 // Note that mLogger.mError might be true too at this point.
8690 mLogger .onStopped ();
8791
88- SynthesisMessageParams token = null ;
92+ SynthesisMessageParams token ;
8993 synchronized (mStateLock ) {
9094 if (mStopped ) {
9195 Log .w (TAG , "stop() called twice" );
9296 return ;
9397 }
9498
95- // mToken will be null if the engine encounters
96- // an error before it called start().
97- if (mToken == null ) {
98- // In all other cases, mAudioTrackHandler.stop() will
99- // result in onComplete being called.
100- mLogger .onWriteData ();
101- } else {
102- token = mToken ;
103- }
99+ token = mToken ;
104100 mStopped = true ;
105101 }
106102
@@ -109,7 +105,24 @@ void stop() {
109105 // point it will write an additional buffer to the token - but we
110106 // won't worry about that because the audio playback queue will be cleared
111107 // soon after (see SynthHandler#stop(String).
108+ token .setIsError (wasError );
112109 token .clearBuffers ();
110+ if (wasError ) {
111+ // Also clean up the audio track if an error occurs.
112+ mAudioTrackHandler .enqueueSynthesisDone (token );
113+ }
114+ } else {
115+ // This happens when stop() or error() were called before start() was.
116+
117+ // In all other cases, mAudioTrackHandler.stop() will
118+ // result in onSynthesisDone being called, and we will
119+ // write data there.
120+ mLogger .onWriteData ();
121+
122+ if (wasError ) {
123+ // We have to dispatch the error ourselves.
124+ mDispatcher .dispatchOnError ();
125+ }
113126 }
114127 }
115128
@@ -219,7 +232,7 @@ public void error() {
219232 // Currently, this call will not be logged if error( ) is called
220233 // before start.
221234 mLogger .onError ();
222- stop ( );
235+ stopImpl ( true );
223236 }
224237
225238}
0 commit comments