From 6f62ef32e2dd4e7935f5a2d201ad5f3b78b4a203 Mon Sep 17 00:00:00 2001 From: Teng-Hui Zhu Date: Thu, 24 May 2012 14:53:28 -0700 Subject: [PATCH] Better support for HTML5 audio loop. Loop is trigger by a seek to 0 when ended on native side but there is no play call. So on java side, we detect this and call into native side to trigger a play after completion. This fixed the UI problem and keep in sync with the native mode. Beyond that, we don't need to reload for looping and we don't have the seek to play artifacts. bug:5461143 webkit change: https://android-git.corp.google.com/g/#/c/193750/ Change-Id: I779f3e1fbc789832a1a99d1f17823db6b57b35df --- core/java/android/webkit/HTML5Audio.java | 28 +++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/core/java/android/webkit/HTML5Audio.java b/core/java/android/webkit/HTML5Audio.java index 689884f7aba9e..fc5df2d5f59cb 100644 --- a/core/java/android/webkit/HTML5Audio.java +++ b/core/java/android/webkit/HTML5Audio.java @@ -67,6 +67,8 @@ class HTML5Audio extends Handler private String mUrl; private boolean mAskToPlay = false; + private boolean mLoopEnabled = false; + private boolean mProcessingOnEnd = false; private Context mContext; // Timer thread -> UI thread @@ -143,7 +145,13 @@ class HTML5Audio extends Handler // MediaPlayer.OnCompletionListener; public void onCompletion(MediaPlayer mp) { mState = COMPLETE; + mProcessingOnEnd = true; nativeOnEnded(mNativePointer); + mProcessingOnEnd = false; + if (mLoopEnabled == true) { + nativeOnRequestPlay(mNativePointer); + mLoopEnabled = false; + } } // MediaPlayer.OnErrorListener @@ -264,14 +272,10 @@ class HTML5Audio extends Handler private void play() { - if (mState == COMPLETE) { + if (mState == COMPLETE && mLoopEnabled == true) { // Play it again, Sam - mTimer.cancel(); - mTimer = new Timer(); - mAskToPlay = true; - mMediaPlayer.stop(); - mState = STOPPED; - mMediaPlayer.prepareAsync(); + mMediaPlayer.start(); + mState = STARTED; return; } @@ -304,14 +308,11 @@ class HTML5Audio extends Handler } private void seek(int msec) { + if (mProcessingOnEnd == true && mState == COMPLETE && msec == 0) { + mLoopEnabled = true; + } if (mState >= PREPARED) { mMediaPlayer.seekTo(msec); - if (mState == COMPLETE) { - // Seeking after the stream had completed will - // cause us to start playing again. This is to - // support audio tags that specify loop=true. - play(); - } } } @@ -336,6 +337,7 @@ class HTML5Audio extends Handler private native void nativeOnBuffering(int percent, int nativePointer); private native void nativeOnEnded(int nativePointer); + private native void nativeOnRequestPlay(int nativePointer); private native void nativeOnPrepared(int duration, int width, int height, int nativePointer); private native void nativeOnTimeupdate(int position, int nativePointer);