Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid's MediaPlayer setSurface while on paused state
    primarykey
    data
    text
    <h2><strong>EDIT</strong>:</h2> <p>So apparently this has nothing to do with multiple activities, and this has something to do with the encoding of the video file.</p> <p>I will try to simplify the question: I have a MediaPlayer object in a paused state, when I call <code>mediaPlayer.getCurrentPosition()</code> I get a result which is accurate. When I call <code>mediaPlayer.setSurface()</code> (using a different surface) followed by <code>mediaPlayer.play()</code> on this object the video is played in a different position than the one returned by getCurrentPosition(). I am testing on API >= ICE_CREAM_SANDWICH.</p> <ul> <li>Tested both using a local resource in the project and via network stream, result: no difference.</li> </ul> <p>Link to video file: <a href="http://wpc.4ba9.edgecastcdn.net/804BA9/testenvmedia/m_8705_LuS3w7ctSZjB.mov.bs.mp4" rel="nofollow">http://wpc.4ba9.edgecastcdn.net/804BA9/testenvmedia/m_8705_LuS3w7ctSZjB.mov.bs.mp4</a></p> <hr> <p>I have two activities, Activity-A and Activity-B.</p> <p>Activity-A plays video from a remote stream to a <code>TextureView</code> using a <code>MediaPlayer</code>, it also holds a button, who's purpose is to:</p> <ol> <li><p>Call mediaPlayer.pause();</p></li> <li><p>Open Activity-B.</p></li> </ol> <p>Activity-B holds also a <code>TextureView</code> which is supposed to play the same video at it's current location using the same <code>MediaPlayer</code> object.</p> <p>Activity-A onCreate method:</p> <pre><code> textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int w, int h) { try { final MediaPlayer mediaPlayer = MyApplication.getMediaPlayer(); mediaPlayer.setDataSource(context, videoURI); mediaPlayer.setSurface(new Surface(surfaceTexture)); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setLooping(shouldLoop); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.start(); } }); mediaPlayer.prepareAsync(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {} @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) {} }); btnFullScreen.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Log.e("WHATEVER", "&gt;&gt;&gt; pause video at " + MyApplication.getMediaPlayer().getCurrentPosition()); MyApplication.getMediaPlayer().pause(); Intent intent = new Intent(getActivity(), ActivityB.class); startActivity(intent); }}); </code></pre> <hr> <p>Activity-B onCreate method:</p> <pre><code> textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int w, int h) { MediaPlayer mediaPlayer = MyApplication.getMediaPlayer(); mediaPlayer.setSurface(new Surface(surface)); Log.e("WHATEVER", "&gt;&gt;&gt; resume video at " + mediaPlayer.getCurrentPosition()); mediaPlayer.start(); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } }); </code></pre> <p>LogCat output:</p> <pre><code>10-10 17:33:15.966 13886-13886/com.whatever.android.debug E/WHATEVER﹕ &gt;&gt;&gt; pause video at 1958 10-10 17:33:16.817 13886-13886/com.whatever.android.debug E/WHATEVER﹕ &gt;&gt;&gt; resume video at 1958 </code></pre> <hr> <p>So even though it seems like the MediaPlayer is going to resume from that exact position, I get different behaviour for different videos, most of them start at the exact same frame every time I try to play those in Activity-B, one video starts at the very beginning, other one starts at some other point every time.</p> <p>From Android's documentation of <code>MediaPlayer.SetSurface()</code> method:</p> <p><code>This method can be called in any state and calling it does not change the object state.</code></p> <p>Any help would be greatly appreciated. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload