安卓VideoView恢复播放无法工作

3

我一直在研究如何在Android中使用VideoView播放视频。

在片段的OnPause中,我会暂停视频并存储当前的播放进度。

    @Override
    public void onPause() {
            super.onPause();

        if (videoview != null && videoview.isPlaying()) {
            videoview.pause();
            seekTime = videoview.getCurrentPosition();

        }
    }

在OnResume中,我正在尝试使用以下方法恢复视频 -

        @Override
        public void onResume() {
            super.onResume();
            videoview.seekTo(seekTime);
            //vvOnboardingVideos.resume();// this is not working for now - need to investigate
            videoview.start();   
        }

视频从开头播放。
我有什么做错了吗?请给出建议。

可能是 https://dev59.com/ymw05IYBdhLWcg3wXAmF 的重复问题。 - Gopal
你找到答案了吗? - Vadim Kotov
2个回答

1
尝试反转操作顺序:
@Override
public void onResume() {
    super.onResume();
    videoview.start();
    videoview.seekTo(seekTime);   
}

这将产生相同的结果 - 视频从开头开始播放。 - Vadim Kotov

0

试试这个:

当调用onpause()时,保存视频位置

// This gets called before onPause
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        stopPosition = videoView.getCurrentPosition(); 
        videoView.pause();
        outState.putInt("video_position", stopPosition);
    } 

然后在你的onCreate()中从包中加载stopPosition

@Override
    protected void onCreate(Bundle args) {
        super.onCreate(args);
        if( args != null ) {
            stopPosition = args.getInt("video_position");
        }
    }

使用以下方法恢复视频:

@Override
    public void onResume() {
        super.onResume();
        Log.d("Tag", "onResume");
        videoView.seekTo(stopPosition);
        videoView.start(); //Or use videoView.resume() if it doesn't work.
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接