MediaPlayer: add mPreparing to weed out unwanted prepared messages

Bug: 21266735
Change-Id: Ie4fe76533c9b7f505c57ba63df7992f2490942cc
This commit is contained in:
Robert Shih
2015-05-21 15:15:50 -07:00
committed by The Android Automerger
parent 16310ffdb9
commit 2c838fbd87
2 changed files with 32 additions and 3 deletions

View File

@@ -77,6 +77,7 @@ import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicBoolean;
import java.lang.ref.WeakReference;
/**
@@ -623,6 +624,9 @@ public class MediaPlayer implements SubtitleController.Listener
private int mUsage = -1;
private boolean mBypassInterruptionPolicy;
// use AtomicBoolean instead of boolean so we can use the same member both as a flag and a lock.
private AtomicBoolean mPreparing = new AtomicBoolean();
/**
* Default constructor. Consider using one of the create() methods for
* synchronously instantiating a MediaPlayer from a Uri or resource.
@@ -1162,6 +1166,10 @@ public class MediaPlayer implements SubtitleController.Listener
* @throws IllegalStateException if it is called in an invalid state
*/
public void prepare() throws IOException, IllegalStateException {
// The synchronous version of prepare also recieves a MEDIA_PREPARED message.
synchronized (mPreparing) {
mPreparing.set(true);
}
_prepare();
scanInternalSubtitleTracks();
}
@@ -1178,7 +1186,14 @@ public class MediaPlayer implements SubtitleController.Listener
*
* @throws IllegalStateException if it is called in an invalid state
*/
public native void prepareAsync() throws IllegalStateException;
public void prepareAsync() throws IllegalStateException {
synchronized (mPreparing) {
mPreparing.set(true);
}
_prepareAsync();
}
private native void _prepareAsync() throws IllegalStateException;
/**
* Starts or resumes playback. If playback had previously been paused,
@@ -1229,6 +1244,9 @@ public class MediaPlayer implements SubtitleController.Listener
* initialized.
*/
public void stop() throws IllegalStateException {
synchronized (mPreparing) {
mPreparing.set(false);
}
stayAwake(false);
_stop();
}
@@ -1658,6 +1676,9 @@ public class MediaPlayer implements SubtitleController.Listener
* at the same time.
*/
public void release() {
synchronized (mPreparing) {
mPreparing.set(false);
}
stayAwake(false);
updateSurfaceScreenOn();
mOnPreparedListener = null;
@@ -1684,6 +1705,9 @@ public class MediaPlayer implements SubtitleController.Listener
* data source and calling prepare().
*/
public void reset() {
synchronized (mPreparing) {
mPreparing.set(false);
}
mSelectedSubtitleTrackIndex = -1;
synchronized(mOpenSubtitleSources) {
for (final InputStream is: mOpenSubtitleSources) {
@@ -2804,7 +2828,12 @@ public class MediaPlayer implements SubtitleController.Listener
}
switch(msg.what) {
case MEDIA_PREPARED:
scanInternalSubtitleTracks();
synchronized (mPreparing) {
if (mPreparing.get()) {
scanInternalSubtitleTracks();
mPreparing.set(false);
}
}
if (mOnPreparedListener != null)
mOnPreparedListener.onPrepared(mMediaPlayer);
return;

View File

@@ -1045,7 +1045,7 @@ static JNINativeMethod gMethods[] = {
{"_setDataSource", "(Landroid/media/MediaDataSource;)V",(void *)android_media_MediaPlayer_setDataSourceCallback },
{"_setVideoSurface", "(Landroid/view/Surface;)V", (void *)android_media_MediaPlayer_setVideoSurface},
{"_prepare", "()V", (void *)android_media_MediaPlayer_prepare},
{"prepareAsync", "()V", (void *)android_media_MediaPlayer_prepareAsync},
{"_prepareAsync", "()V", (void *)android_media_MediaPlayer_prepareAsync},
{"_start", "()V", (void *)android_media_MediaPlayer_start},
{"_stop", "()V", (void *)android_media_MediaPlayer_stop},
{"getVideoWidth", "()I", (void *)android_media_MediaPlayer_getVideoWidth},