Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing video from the pause state in VideoView

For custom videoview I had to override the MediaController (to change the design of the buttons and connect to the buttons pause / play a selector). The situation is this: put the video on pause and turning the application, and then again restored, the video starts playing again, and not from the point at which it was suspended, and therefore the question arose: how to make the video starts playing from the place , where it was stopped? What do I need to change in the 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);
    }
}
like image 347
ricky Avatar asked Sep 27 '12 08:09

ricky


3 Answers

I think the problem is that videoView.getCurrentPosition() will always return 0 when you call inside onSaveInstanceState(Bundle outState), cos at that time the video already reset, so call videoView.getCurrentPosition() when onPause(), this will return you the desired value.

like image 136
jaredzhang Avatar answered Nov 06 '22 00:11

jaredzhang


I had the same problem. I thought I did everything right but it didnt work. Then I just changed the order so I first call VideoView.seekTo() and then VideoView.start() . That worked. I also have VideoView.requestFocus() in there but I dont know if that matters.

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

So if youre sure you position variable has the right value, this might be your answer. You may not be able to seek when the Video is not loaded. Implement onPreparedListener like this:

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();
}
}
like image 31
Yalla T. Avatar answered Nov 06 '22 00:11

Yalla T.


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);
    }
like image 2
Keshav Gera Avatar answered Nov 05 '22 23:11

Keshav Gera