Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从停止的位置启动视频_Android_Video - Fatal编程技术网

Android 如何从停止的位置启动视频

Android 如何从停止的位置启动视频,android,video,Android,Video,我正在使用VideoView播放视频。如果我退出应用程序,在返回到应用程序ie的onResume()中时,它应该从停止的位置播放视频。要获取当前进度(请在onPause中选中此项): 要恢复(在onResume中): 在onPause()中,保存播放器的当前位置,例如在共享首选项中。在onResume()中,检索该值,然后使用MediaPlayer.seekTo()定位 谢谢@Zelleriation。但我有一个问题,我在横向模式和纵向模式下播放视频。当我切换到横向模式到纵向模式或反之亦然时,

我正在使用VideoView播放视频。如果我退出应用程序,在返回到应用程序ie的onResume()中时,它应该从停止的位置播放视频。

要获取当前进度(请在onPause中选中此项):

要恢复(在onResume中):

在onPause()中,保存播放器的当前位置,例如在共享首选项中。在onResume()中,检索该值,然后使用MediaPlayer.seekTo()定位


谢谢@Zelleriation。但我有一个问题,我在横向模式和纵向模式下播放视频。当我切换到横向模式到纵向模式或反之亦然时,视频从开始处开始。seekTo()方法的类型为int,不应使用long.hi@Zelleriation我这样做了,但没有成功。请在这里帮助我,我尝试搜索答案,最终找到了解决方案。只需在清单文件中添加“android:configChanges=“orientation”。在方向更改后,活动将不会重新启动。最好使用
onSaveInstanceState()
而不是共享首选项。
long progress = mVideoView.getCurrentPosition(); 
mVideoView.seekTo(progress);
@Override
protected void onPause() {

    Log.d(App.TAG, "onPause called");

    if(mMediaPlayer==null){
        Log.d(App.TAG, "Returning from onPause because the mediaplayer is null");
        super.onPause();
        return;
    }

    // the OS is pausing us, see onResume() for resume logic
    settings = getSharedPreferences(Dawdle.TAG, MODE_PRIVATE);
    SharedPreferences.Editor ed = settings.edit();
    mMediaPlayer.pause();
    ed.putInt("LAST_POSITION", mMediaPlayer.getCurrentPosition());  // remember where we are
    ed.putBoolean("PAUSED", true); 
    ed.commit();
    Log.d(App.TAG, "LAST_POSITION saved:" + mMediaPlayer.getCurrentPosition());

    super.onPause();
    releaseMediaPlayer();

}

@Override
public void onResume() {
    Log.d(App.TAG, "onResume called");

    try {

        if (mMediaPlayer==null){
            setupMediaPlayer();
        }

        // if we were paused (set in this.onPause) then resume from the last position
        settings = getSharedPreferences(Dawdle.TAG, MODE_PRIVATE);
        if (settings.getBoolean("PAUSED", false)) {
            // resume from the last position
            startPosition= settings.getInt("LAST_POSITION", 0);
            Log.d(App.TAG,"Seek to last position:" + startPosition);
        }

        mMediaPlayer.setDataSource(path);
        mMediaPlayer.setDisplay(holder);

        // this is key, the call will return immediately and notify this when the player is prepared through a callback to onPrepared
        // so we do not block on the UI thread - do not call any media playback methods before the onPrepared callback
        mMediaPlayer.prepareAsync();  

    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void startVideoPlayback() {
    Log.v(App.TAG, "startVideoPlayback at position:" + startPosition);
    mMediaPlayer.seekTo(startPosition);
    mMediaPlayer.start();
}