【问题标题】:How to start video from where it stopped如何从停止的地方开始播放视频
【发布时间】:2012-06-05 11:13:27
【问题描述】:

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

【问题讨论】:

    标签: android video


    【解决方案1】:

    获取当前进度(在 onPause 中查看):

    long progress = mVideoView.getCurrentPosition(); 
    

    恢复(在 onResume 中):

    mVideoView.seekTo(progress);
    

    【讨论】:

    • 感谢@Zelleriation。但我有一个问题,我正在以横向模式和纵向模式播放视频。当我切换到横向模式到纵向模式或反之亦然时,视频从头开始。
    • seekTo() 方法采用 int 类型,不应使用 long。
    • 嗨@Zelleriation 我做了这个但没有工作请在这里帮助我stackoverflow.com/questions/61824051/…
    【解决方案2】:

    在 onPause() 中,保存播放器的当前位置,例如在共享偏好中。在 onResume() 中取回值,然后使用 MediaPlayer.seekTo() 定位。

    http://developer.android.com/reference/android/media/MediaPlayer.html#seekTo(int)

    @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();
    }
    

    【讨论】:

    • 我尝试寻找答案,终于得到了解决方案。只需在 Manifest 文件中添加 `android:configChanges="orientation`。更改方向后 Activity 不会重新启动。
    • 使用onSaveInstanceState() 可能比共享首选项更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    相关资源
    最近更新 更多