3

I like this site very much because I can find many useful solutions help me very much especially I'm beginner in android programming and this is my first question so I hope find helping from this great community.

The Problem: I play video in my app and make it playing in full screen in landscape when user orient screen so I want to make video restart playing from the same point by using seekTo() and getCurrentPosition() - but the problem that video play many seconds after the position I mean that video seek to same point in time but when start play it add many seconds.

My Code:

    //initialise everything  
protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        v = (VideoView) findViewById(R.id.videoView1);
                v.setVideoPath(dir + videoName + ".3gp");
                v.setMediaController(new MediaController(this));
                if (savedInstanceState == null){
                    v.start();  
                }
                else {
                    playingTime = savedInstanceState.getInt("restartTime", 0);
                    v.seekTo(playingTime);
                }
       }

@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    if (file.exists()){
        playingTime = v.getCurrentPosition();
        v.stopPlayback();
        outState.putInt("restartTime", playingTime);
    }
}

I try many solution such as this How to keep playing video while changing to landscape mode android

I'm using Galaxy Nexus to test my app .. I would be grateful for any hint. Sorry for my bad English and thanks very much.

Community
  • 1
  • 1
Mohamed ElSawaf
  • 229
  • 2
  • 8

1 Answers1

5

In your activity, keep everything normal. It boils down to saving the position when activity is recreated. Here is one approach:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //initialise everything


    //finally
    if(savedInstanceState!=null) {
        int seekValue = savedInstanceState.getInt("where");
        //now seekTo(seekValue)
    }
}
@Override
    protected void onSaveInstanceState(Bundle outState) {
        int seekValue; //set this to the current position;
        outState.putInt("where", seekValue);
        super.onSaveInstanceState(outState);
    }
}
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • Wouldn't you call `super.onSaveInstanceState(outState)` last in your `Activity` `onSaveInstanceState(...)` method? In other words AFTER calling `outState.PutInt(...)`. – Squonk Sep 22 '12 at 17:58
  • @Squonk makes sense from a semantic point of view but it actually does not matter since you put your data in the same `outState` instance and the `super` implementation does not require to have the final state of `outState` yet. But I'd prefer to put it first too since it's independant of the super implementation. Although one could argue that super may need to setup `outState` before you can put stuff in it, in which case it would be safer to put your data after super. – zapl Sep 22 '12 at 18:16
  • Well I prefer to call super at the end not to write over your "Activity's" states. However, I wrote this just for demonstration; moving it anyway. – Sherif elKhatib Sep 22 '12 at 18:25
  • @zapl : OK agreed - it is basically just a semantic point. Having looked at the source code for `Activity`, it doesn't seem it will make a difference either way. I guess I just call the `super` implementation last in my own code because it makes the 'flow' look more logical to me. – Squonk Sep 22 '12 at 19:44
  • @SherifelKhatib Thanks very much Mr. Sherif but I actually did that .. Kindly check my code above. – Mohamed ElSawaf Sep 23 '12 at 01:36
  • Can anyone help me? Is it possible this problem from my device Galaxy Nexus? – Mohamed ElSawaf Sep 25 '12 at 11:36