Android的MediaPlayer的setSurface而在暂停状态 [英] Android's MediaPlayer setSurface while on paused state
问题描述
因此很明显,这有没有关系多种活动,这已是与视频文件的编码。
So apparently this has nothing to do with multiple activities, and this has something to do with the encoding of the video file.
我会尽量简化问题:我有一个MediaPlayer对象处于暂停状态,当我打电话 mediaPlayer.getCurrentPosition()
我得到的结果是准确的。当我打电话 mediaPlayer.setSurface()
(使用不同的表面),然后按 mediaPlayer.play()
此对象视频播放比()通过getCurrentPosition返回一个不同的位置。我测试的API> = ICE_CREAM_SANDWICH。
I will try to simplify the question: I have a MediaPlayer object in a paused state, when I call mediaPlayer.getCurrentPosition()
I get a result which is accurate. When I call mediaPlayer.setSurface()
(using a different surface) followed by mediaPlayer.play()
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.
- 在测试了使用本地资源的项目,并通过网络数据流,结果是:没有什么区别
链接到视频文件:的http://wpc.4ba9.edgecastcdn.net/804BA9/testenvmedia/m_8705_LuS3w7ctSZjB.mov.bs.mp4
我有两个活动,活动-A和活动-B。
I have two activities, Activity-A and Activity-B.
活动-A播放视频从远程流到 TextureView
使用的MediaPlayer
,
它也拥有一个按钮,谁的宗旨是:
Activity-A plays video from a remote stream to a TextureView
using a MediaPlayer
,
it also holds a button, who's purpose is to:
-
呼叫mediaPlayer.pause();
Call mediaPlayer.pause();
打开活动-B。
活动-B还持有 TextureView
这是为了起到同样的视频在它的使用相同的 MediaPlayer的当前位置
对象。
Activity-B holds also a TextureView
which is supposed to play the same video at it's current location using the same MediaPlayer
object.
活动-A onCreate方法:
Activity-A onCreate method:
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", ">>> pause video at " + MyApplication.getMediaPlayer().getCurrentPosition());
MyApplication.getMediaPlayer().pause();
Intent intent = new Intent(getActivity(), ActivityB.class);
startActivity(intent);
}});
活动-B onCreate方法:
Activity-B onCreate method:
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", ">>> 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) {
}
});
LogCat中输出:
LogCat output:
10-10 17:33:15.966 13886-13886/com.whatever.android.debug E/WHATEVER﹕ >>> pause video at 1958
10-10 17:33:16.817 13886-13886/com.whatever.android.debug E/WHATEVER﹕ >>> resume video at 1958
所以,即使它似乎是在MediaPlayer会从确切位置恢复,我得到了不同的影片不同的行为,每一次我尝试播放那些活动-B大部分开始在完全相同的框架,人们视频开始在开始的时候,另外一个每次都开始于一些其他的点。
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.
在 MediaPlayer.SetSurface Android的文档()
方法:
这种方法可以称为在任何状态下,把它不会改变对象的状态。
任何帮助将是很大的AP preciated。 谢谢!
Any help would be greatly appreciated. Thanks!
推荐答案
看来你的问题是这个特定的视频,更多的关键帧,将帮助玩家更准确地恢复播放。
it seems your problem is with this specific video, more keyframes will help the player to resume the playing more accurately.
尝试重新连接code再次,有关ffmpeg和libx264尝试将这些参数:
try to re-encode again, with ffmpeg and libx264 try adding these parameters:
-g=25 -keyint_min=25
这篇关于Android的MediaPlayer的setSurface而在暂停状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!