我在Galaxy Tab 3上的Android 4.4上播放视频时遇到问题。以前的Android版本4.2没有出现此问题。
问题
我正在循环播放视频。这对所有用户都非常有效,直到Tab 3更新到Android 4.4。从那以后,视频在第一个循环之后冻结(它准确地卡在视频的第一帧上)。
我可以重现这种行为,并且当视频冻结时,我的LogCat开始充满以下输出:
16:25:25.239 14589-14601/my.app V/MediaPlayer? back from callback
16:25:25.499 14589-14686/my.app V/MediaPlayer? message received msg=7, ext1=0, ext2=0
16:25:25.499 14589-14686/my.app V/MediaPlayer? unrecognized message: (7, 0, 0)
16:25:25.499 14589-14686/my.app V/MediaPlayer? callback application
16:25:25.499 14589-14686/my.app V/MediaPlayer? back from callback
16:25:25.519 14589-14602/my.app V/MediaPlayer? message received msg=4, ext1=0, ext2=0
16:25:25.519 14589-14602/my.app V/MediaPlayer? Received seek complete
16:25:25.519 14589-14602/my.app V/MediaPlayer? All seeks complete - return to regularly scheduled program
16:25:25.519 14589-14602/my.app V/MediaPlayer? callback application
16:25:25.519 14589-14602/my.app V/MediaPlayer? back from callback
16:25:25.519 14589-14601/my.app V/MediaPlayer? message received msg=6, ext1=0, ext2=0
16:25:25.519 14589-14601/my.app V/MediaPlayer? Received MEDIA_STARTED
16:25:25.519 14589-14601/my.app V/MediaPlayer? callback application
16:25:25.519 14589-14601/my.app V/MediaPlayer? back from callback
16:25:25.789 14589-14686/my.app V/MediaPlayer? message received msg=7, ext1=0, ext2=0
16:25:25.789 14589-14686/my.app V/MediaPlayer? unrecognized message: (7, 0, 0)
16:25:25.789 14589-14686/my.app V/MediaPlayer? callback application
16:25:25.789 14589-14686/my.app V/MediaPlayer? back from callback
16:25:25.809 14589-14602/my.app V/MediaPlayer? message received msg=4, ext1=0, ext2=0
16:25:25.809 14589-14602/my.app V/MediaPlayer? Received seek complete
代码
以下(简单的)活动应该播放视频。永远不会调用MediaPlayer.OnErrorListener()和MediaPlayer.OnInfoListener()。
public class VideoActivity extends Activity {
private MediaPlayer mediaPlayer;
private String videoPath = "some path obtained from the system";
// [...]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mediaPlayer = new MediaPlayer();
startMovie();
}
private void startMovie(){
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.setLooping(true);
mediaPlayer.setDataSource(videoPath);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mp.start();
mp.seekTo(0);
}
});
mediaPlayer.prepare();
}
}
在活动的生命周期内,视频可以使用不同的视频文件路径多次启动,这就是我在开始播放电影之前停止并重置播放器的原因。
到目前为止,我在互联网上找到的唯一解决方案是确保GC不收集MediaPlayer(我不会将其作为方法本地对象),并实现WakeLock,我也没有效果
有谁可以帮助我,并指出我正确的方向?谢谢!
答案 0 :(得分:3)
更多解决方法而非解决方案,我最终使用setLooping
MediaPlayer
方法 循环播放视频。相反,我现在正在OnCompletion
MediaPlayer
回调中重新启动视频。经过大量的尝试,这是我在我测试的所有设备上工作的唯一方法。
public class VideoActivity extends Activity {
private MediaPlayer mediaPlayer;
private String videoPath = "some path obtained from the system";
// [...]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mediaPlayer = new MediaPlayer();
startMovie();
}
private void startMovie(){
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.setDataSource(videoPath);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mp.start();
mp.seekTo(0);
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
startMovie();
}
});
mediaPlayer.prepare();
}
}
但是,如果某人有解决该问题的有效方法,我将很乐意将相应的答案标记为正确。