diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 52f04148..32614a28 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -17,7 +17,7 @@ + * Called from any thread other than the PlayTask thread. + */ + public void waitForStop() { + synchronized (mStopLock) { + while (!mStopped) { + try { + mStopLock.wait(); + } catch (InterruptedException ie) { + // discard + } + } + } + } + @Override public void run() { try { @@ -467,6 +487,12 @@ public void run() { } catch (IOException ioe) { throw new RuntimeException(ioe); } finally { + // tell anybody waiting on us that we're done + synchronized (mStopLock) { + mStopped = true; + mStopLock.notifyAll(); + } + // Send message through Handler so it runs on the right thread. mLocalHandler.sendMessage( mLocalHandler.obtainMessage(MSG_PLAY_STOPPED, mFeedback)); diff --git a/src/com/android/grafika/PlayMovieActivity.java b/src/com/android/grafika/PlayMovieActivity.java index 89f8900f..988e87fd 100644 --- a/src/com/android/grafika/PlayMovieActivity.java +++ b/src/com/android/grafika/PlayMovieActivity.java @@ -57,6 +57,8 @@ public class PlayMovieActivity extends Activity implements OnItemSelectedListene private MoviePlayer.PlayTask mPlayTask; private boolean mSurfaceTextureReady = false; + private final Object mStopper = new Object(); // used to signal stop + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -93,7 +95,13 @@ protected void onPause() { // We're not keeping track of the state in static fields, so we need to shut the // playback down. Ideally we'd preserve the state so that the player would continue // after a device rotation. - stopPlayback(); + // + // We want to be sure that the player won't continue to send frames after we pause, + // because we're tearing the view down. So we wait for it to stop here. + if (mPlayTask != null) { + stopPlayback(); + mPlayTask.waitForStop(); + } } @Override @@ -184,12 +192,11 @@ public void clickPlayStop(@SuppressWarnings("unused") View unused) { } /** - * Requests stoppage if a movie is currently playing. + * Requests stoppage if a movie is currently playing. Does not wait for it to stop. */ private void stopPlayback() { if (mPlayTask != null) { mPlayTask.requestStop(); - mPlayTask = null; } } diff --git a/src/com/android/grafika/PlayMovieSurfaceActivity.java b/src/com/android/grafika/PlayMovieSurfaceActivity.java index 84c20a4b..3cd2abd0 100644 --- a/src/com/android/grafika/PlayMovieSurfaceActivity.java +++ b/src/com/android/grafika/PlayMovieSurfaceActivity.java @@ -115,7 +115,13 @@ protected void onPause() { // We're not keeping track of the state in static fields, so we need to shut the // playback down. Ideally we'd preserve the state so that the player would continue // after a device rotation. - stopPlayback(); + // + // We want to be sure that the player won't continue to send frames after we pause, + // because we're tearing the view down. So we wait for it to stop here. + if (mPlayTask != null) { + stopPlayback(); + mPlayTask.waitForStop(); + } } @Override @@ -210,7 +216,6 @@ public void clickPlayStop(@SuppressWarnings("unused") View unused) { private void stopPlayback() { if (mPlayTask != null) { mPlayTask.requestStop(); - mPlayTask = null; } }