Skip to content

Commit 817d6d5

Browse files
author
Ben Murdoch
committed
Check MediaPlayer state, do not teardown() from UI thread.
The teardown() function should only be called from the native thread when WebCore is happy for us to release the media player. When we lose audio focus, simply stop playback. Also be careful not to check the playing state of the MediaPlayer if we are in the error state. Bug: 5461143 Change-Id: Iba03bdad5c39b104b3071129430d7ef2177e9358
1 parent 9ad680c commit 817d6d5

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

core/java/android/webkit/HTML5Audio.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,24 +238,27 @@ public void onAudioFocusChange(int focusChange) {
238238
switch (focusChange) {
239239
case AudioManager.AUDIOFOCUS_GAIN:
240240
// resume playback
241-
if (mMediaPlayer == null) resetMediaPlayer();
242-
else if (!mMediaPlayer.isPlaying()) mMediaPlayer.start();
243-
mState = STARTED;
241+
if (mMediaPlayer == null) {
242+
resetMediaPlayer();
243+
} else if (mState != ERROR && !mMediaPlayer.isPlaying()) {
244+
mMediaPlayer.start();
245+
mState = STARTED;
246+
}
244247
break;
245248

246249
case AudioManager.AUDIOFOCUS_LOSS:
247-
// Lost focus for an unbounded amount of time: stop playback and release media player
248-
if (mMediaPlayer.isPlaying()) mMediaPlayer.stop();
249-
mMediaPlayer.release();
250-
mMediaPlayer = null;
250+
// Lost focus for an unbounded amount of time: stop playback.
251+
if (mState != ERROR && mMediaPlayer.isPlaying()) {
252+
mMediaPlayer.stop();
253+
mState = STOPPED;
254+
}
251255
break;
252256

253257
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
254258
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
255259
// Lost focus for a short time, but we have to stop
256-
// playback. We don't release the media player because playback
257-
// is likely to resume
258-
if (mMediaPlayer.isPlaying()) mMediaPlayer.pause();
260+
// playback.
261+
if (mState != ERROR && mMediaPlayer.isPlaying()) pause();
259262
break;
260263
}
261264
}
@@ -273,10 +276,7 @@ private void play() {
273276
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
274277
AudioManager.AUDIOFOCUS_GAIN);
275278

276-
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
277-
// could not get audio focus.
278-
teardown();
279-
} else {
279+
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
280280
mMediaPlayer.start();
281281
mState = STARTED;
282282
}
@@ -299,8 +299,13 @@ private void seek(int msec) {
299299
}
300300
}
301301

302+
/**
303+
* Called only over JNI when WebKit is happy to
304+
* destroy the media player.
305+
*/
302306
private void teardown() {
303307
mMediaPlayer.release();
308+
mMediaPlayer = null;
304309
mState = ERROR;
305310
mNativePointer = 0;
306311
}

0 commit comments

Comments
 (0)