在VideoView中从暂停状态播放视频

时间:2012-09-27 08:33:08

标签: android android-videoview

对于自定义视频视图,我必须覆盖MediaController(更改按钮的设计并连接到按钮暂停/播放选择器)。情况是这样的:将视频暂停并转动应用程序,然后再次恢复,视频再次开始播放,而不是从它被暂停的点开始播放,因此出现了问题:如何让视频开始播放从哪里停下来的地方?我需要在VideoActivity中更改什么?

public class VideoActivity extends Activity {

VideoView videoView;

VkMediaController mc;

private static final String CURRENT = "duration";

private static final String URL = "url";

private Uri mURI;

private int mCurrentPosition = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.video_view_player);
    videoView = (VideoView) findViewById(R.id.videoViewplayer);
    mc = new VkMediaController(this);
    mURI = getIntent().getData();
    if (savedInstanceState != null) {
        mURI = Uri.parse(savedInstanceState.getString(URL));
        mCurrentPosition = savedInstanceState.getInt(CURRENT);

    }
    videoView.setMediaController(mc);
    videoView.setVideoURI(mURI);
    videoView.requestFocus();
}

@Override
protected void onStart() {
    super.onStart();
    videoView.start();
    if (mCurrentPosition != -1) {
        videoView.seekTo(mCurrentPosition);
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt(CURRENT, videoView.getCurrentPosition());
    outState.putString(URL, mURI.toString());
    super.onSaveInstanceState(outState);
}

}

3 个答案:

答案 0 :(得分:17)

我认为问题是videoView.getCurrentPosition()onSaveInstanceState(Bundle outState)内拨打电话时始终会返回0,此时视频已经重置,因此在videoView.getCurrentPosition()时拨打onPause() ,这将返回所需的值。

答案 1 :(得分:2)

我遇到了同样的问题。我以为我做的一切都没事,但它没有用。然后我只是更改了订单,因此我先调用VideoView.seekTo()然后调用VideoView.start()。那很有效。 我也有VideoView.requestFocus(),但我不知道这是否重要。

videoPlayer.requestFocus();
videoPlayer.seekTo(position);
videoPlayer.start();

因此,如果您确定您的位置变量具有正确的值,那么这可能是您的答案。 您可能无法在未加载视频时进行搜索。像这样实现onPreparedListener:

public class VideoActivity extends Activity implements OnPreparedListener {
    VideoView videoPlayer;
    int position;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState!=null){
    position=savedInstanceState.getInt("pos")
    }
    // setting up the videoview
    setContentView(R.layout.activity_video);
    getIntent().getExtras().getString("url");
    Uri videouri = Uri.parse(getIntent().getExtras().getString("url"));
    videoPlayer = (VideoView) findViewById(R.id.videoView);
    videoPlayer.setOnPreparedListener(this);
    videoPlayer.setKeepScreenOn(true);
    videoPlayer.setVideoURI(videouri);

    }

    /**
     * Start the plaback when the video is loaded
     * 
     * @param mp
     * @see android.media.MediaPlayer.OnPreparedListener#onPrepared(android.media.MediaPlayer)
     */
    public void onPrepared(MediaPlayer mp) {
    //this is a TextView infront of the VideoView which tells the User the Video is loading- hide that
    findViewById(R.id.tv_video_load).setVisibility(View.GONE);

    videoPlayer.requestFocus();
    videoPlayer.seekTo(position)
    videoPlayer.start();
}
}

答案 2 :(得分:0)

Portrait to LandScape LifeCycle Demo

    VideoView videoView;
    int lastPosition;
    int Video_STATE_POSITION  = 0;

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

        if (savedInstanceState != null)
        {
            String message = savedInstanceState.getString("message");
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();

            lastPosition=savedInstanceState.getInt("STATE_POSITION");
            videoView.seekTo(lastPosition);
            videoView.start();
            Log.e("Keshav", "MainActivity onCreate() is called  savedInstanceState  lastPosition->" + lastPosition);
        }
}

  @Override
    public void onSaveInstanceState(Bundle outState)
    {
        outState.putString("message", "This is my message to be reloaded");
        outState.putInt("STATE_POSITION", Video_STATE_POSITION);
        super.onSaveInstanceState(outState);
    }

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

        Video_STATE_POSITION = videoView.getCurrentPosition();

        Log.e("Keshav", "MainActivity onPause is called  ->" +Video_STATE_POSITION);
    }