@@ -31,11 +31,10 @@ public class HTML5VideoView implements MediaPlayer.OnPreparedListener {
3131 // NOTE: these values are in sync with VideoLayerAndroid.h in webkit side.
3232 // Please keep them in sync when changed.
3333 static final int STATE_INITIALIZED = 0 ;
34- static final int STATE_NOTPREPARED = 1 ;
34+ static final int STATE_PREPARING = 1 ;
3535 static final int STATE_PREPARED = 2 ;
3636 static final int STATE_PLAYING = 3 ;
37- static final int STATE_RELEASED = 4 ;
38- protected int mCurrentState ;
37+ static final int STATE_RESETTED = 4 ;
3938
4039 protected HTML5VideoViewProxy mProxy ;
4140
@@ -46,11 +45,11 @@ public class HTML5VideoView implements MediaPlayer.OnPreparedListener {
4645 // This is used to find the VideoLayer on the native side.
4746 protected int mVideoLayerId ;
4847
49- // Every video will have one MediaPlayer. Given the fact we only have one
50- // SurfaceTexture, there is only one MediaPlayer in action. Every time we
51- // switch videos, a new instance of MediaPlayer will be created in reset() .
52- // Switching between inline and full screen will also create a new instance.
53- protected MediaPlayer mPlayer ;
48+ // Given the fact we only have one SurfaceTexture, we cannot support multiple
49+ // player at the same time. We may recreate a new one and abandon the old
50+ // one at transition time .
51+ protected static MediaPlayer mPlayer = null ;
52+ protected static int mCurrentState = - 1 ;
5453
5554 // We need to save such info.
5655 protected Uri mUri ;
@@ -60,10 +59,12 @@ public class HTML5VideoView implements MediaPlayer.OnPreparedListener {
6059 // See http://www.whatwg.org/specs/web-apps/current-work/#event-media-timeupdate
6160 protected static Timer mTimer ;
6261
62+ protected boolean mPauseDuringPreparing ;
63+
6364 // The spec says the timer should fire every 250 ms or less.
6465 private static final int TIMEUPDATE_PERIOD = 250 ; // ms
66+ private boolean mSkipPrepare = false ;
6567
66- protected boolean mPauseDuringPreparing ;
6768 // common Video control FUNCTIONS:
6869 public void start () {
6970 if (mCurrentState == STATE_PREPARED ) {
@@ -83,7 +84,7 @@ public void start() {
8384 public void pause () {
8485 if (isPlaying ()) {
8586 mPlayer .pause ();
86- } else if (mCurrentState == STATE_NOTPREPARED ) {
87+ } else if (mCurrentState == STATE_PREPARING ) {
8788 mPauseDuringPreparing = true ;
8889 }
8990 // Delete the Timer to stop it since there is no stop call.
@@ -124,11 +125,11 @@ public boolean isPlaying() {
124125 }
125126 }
126127
127- public void release () {
128- if (mCurrentState != STATE_RELEASED ) {
129- mPlayer .release ();
128+ public void reset () {
129+ if (mCurrentState != STATE_RESETTED ) {
130+ mPlayer .reset ();
130131 }
131- mCurrentState = STATE_RELEASED ;
132+ mCurrentState = STATE_RESETTED ;
132133 }
133134
134135 public void stopPlayback () {
@@ -142,9 +143,16 @@ public boolean getPauseDuringPreparing() {
142143 }
143144
144145 // Every time we start a new Video, we create a VideoView and a MediaPlayer
145- public void init (int videoLayerId , int position ) {
146- mPlayer = new MediaPlayer ();
147- mCurrentState = STATE_INITIALIZED ;
146+ public void init (int videoLayerId , int position , boolean skipPrepare ) {
147+ if (mPlayer == null ) {
148+ mPlayer = new MediaPlayer ();
149+ mCurrentState = STATE_INITIALIZED ;
150+ }
151+ mSkipPrepare = skipPrepare ;
152+ // If we want to skip the prepare, then we keep the state.
153+ if (!mSkipPrepare ) {
154+ mCurrentState = STATE_INITIALIZED ;
155+ }
148156 mProxy = null ;
149157 mVideoLayerId = videoLayerId ;
150158 mSaveSeekTime = position ;
@@ -195,17 +203,28 @@ public void setOnInfoListener(HTML5VideoViewProxy proxy) {
195203 }
196204
197205 public void prepareDataCommon (HTML5VideoViewProxy proxy ) {
198- try {
199- mPlayer .setDataSource (proxy .getContext (), mUri , mHeaders );
200- mPlayer .prepareAsync ();
201- } catch (IllegalArgumentException e ) {
202- e .printStackTrace ();
203- } catch (IllegalStateException e ) {
204- e .printStackTrace ();
205- } catch (IOException e ) {
206- e .printStackTrace ();
206+ if (!mSkipPrepare ) {
207+ try {
208+ mPlayer .reset ();
209+ mPlayer .setDataSource (proxy .getContext (), mUri , mHeaders );
210+ mPlayer .prepareAsync ();
211+ } catch (IllegalArgumentException e ) {
212+ e .printStackTrace ();
213+ } catch (IllegalStateException e ) {
214+ e .printStackTrace ();
215+ } catch (IOException e ) {
216+ e .printStackTrace ();
217+ }
218+ mCurrentState = STATE_PREPARING ;
219+ } else {
220+ // If we skip prepare and the onPrepared happened in inline mode, we
221+ // don't need to call prepare again, we just need to call onPrepared
222+ // to refresh the state here.
223+ if (mCurrentState >= STATE_PREPARED ) {
224+ onPrepared (mPlayer );
225+ }
226+ mSkipPrepare = false ;
207227 }
208- mCurrentState = STATE_NOTPREPARED ;
209228 }
210229
211230 public void reprepareData (HTML5VideoViewProxy proxy ) {
@@ -294,10 +313,6 @@ public boolean getReadyToUseSurfTex() {
294313 return false ;
295314 }
296315
297- public SurfaceTexture getSurfaceTexture (int videoLayerId ) {
298- return null ;
299- }
300-
301316 public void deleteSurfaceTexture () {
302317 }
303318
@@ -332,14 +347,14 @@ public boolean fullScreenExited() {
332347 return false ;
333348 }
334349
335- private boolean m_startWhenPrepared = false ;
350+ private boolean mStartWhenPrepared = false ;
336351
337352 public void setStartWhenPrepared (boolean willPlay ) {
338- m_startWhenPrepared = willPlay ;
353+ mStartWhenPrepared = willPlay ;
339354 }
340355
341356 public boolean getStartWhenPrepared () {
342- return m_startWhenPrepared ;
357+ return mStartWhenPrepared ;
343358 }
344359
345360}
0 commit comments