Android 从 onResume 继续播放 VideoView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11960730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:18:31  来源:igfitidea点击:

Resume playing VideoView from onResume

androidandroid-videoview

提问by heero

I have a VideoView and it is pause when I start an Email intent. When the email intent is done, I want the videoView to continue playing, however, it restarts from the beginning.

我有一个 VideoView,当我启动电子邮件意图时它会暂停。完成电子邮件意图后,我希望 videoView 继续播放,但是,它会从头开始重新启动。

@Override
public void onPause() {
    Log.d(TAG, "onPause called");
    super.onPause();
    videoView.pause();
}
@Override
public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume called");
    videoView.start();//resume() doesnt work
}

How can I get the videoView to resume from where it left off.

我怎样才能让 videoView 从它停止的地方恢复。

回答by iTurki

What about this:

那这个呢:

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

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

Then in your onCreate() call the stopPosition from the bundle and set it globally

然后在您的 onCreate() 中从包中调用 stopPosition 并全局设置它

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

回答by Abhishek V

Using seekTogives a slight flickering efeect while resuming the video. Better approach would be using pause()and start()methods of MediaPlayerclass instead of using methods from VideoView. The start()method of VideoViewrestarts the video from the beginning, but start()method of MediaPlayerresumes the video if the pause()method had been previously called.

使用seekTo会在恢复视频时产生轻微的闪烁效果。更好的方法是使用pause()和类的start()方法,MediaPlayer而不是使用来自VideoView. 该start()方法VideoView重新启动从一开始视频,但start()方法MediaPlayer如果视频简历pause()的方法已被以前称作。

Here is what official android doc says

这是官方android文档所说的

public void start ()

Added in API level 1 Starts or resumes playback. If playback had previously been paused, playback will continue from where it was paused. If playback had been stopped, or never started before, playback will start at the beginning.

公共无效开始()

在 API 级别 1 中添加 开始或恢复播放。如果之前已暂停播放,将从暂停的位置继续播放。如果播放已停止或从未开始,播放将从头开始。

You can get the MediaPlayerassociated with a VideoViewin OnPreparedListenerof VideoView.

您可以获得MediaPlayerVideoViewin OnPreparedListenerof相关联的VideoView

public class MainActivity extends Activity {

    private MediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        VideoView videoView = (VideoView) findViewById(R.id.videoView1);
        videoView.setVideoPath(path);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mMediaPlayer = mp;

            }
        });
    }

}

Then subsequent pause()and resume()methods can be called on MediaPlayerobject itself.

然后可以在对象本身上调用后续pause()resume()方法MediaPlayer

//To pause the video
 mMediaPlayer.pause();

//To resume the video
mMediaPlayer.start();

Very simple yet efficient solution. Hope it helps!

非常简单而有效的解决方案。希望能帮助到你!