处理Orientation Change android的视频

时间:2015-08-07 09:34:07

标签: android android-orientation android-videoview

我正在开发一个视频播放器应用。但是当我改变方向时,视频从头开始。我查看了一些解决方案,但大多数人都说要使用

android:configChanges="orientation|screenSize"

但它有助于帮助。 有解决方案吗 谢谢 代码如下:

public class Video2 extends ActionBarActivity {
   final VideoView view = (VideoView) findViewById(R.id.bdayvideoview);
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putInt("current position",view.getCurrentPosition() );
    super.onSaveInstanceState(savedInstanceState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    if(savedInstanceState!=null){
       int currentPos =  savedInstanceState.getInt("current position");
        view.seekTo(currentPos);
    }
    super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.birthdayvideo);
    MediaController mediaController = new MediaController(this);
    String path = "android.resource://" + getPackageName() + "/" + R.raw.video2;
    view.setVideoURI(Uri.parse(path));
    mediaController.setAnchorView(view);
    view.setMediaController(mediaController);

    view.start();

}

}

2 个答案:

答案 0 :(得分:0)

您可以在活动中使用 onSaveInstanceState 来保存电影的当前时间,并使用 onRestoreInstanceState 在同一分钟恢复视频。

答案 1 :(得分:0)

尝试将对savedInstanceState的检查放入onCreate方法。

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.birthdayvideo);
    MediaController mediaController = new MediaController(this);
    String path = "android.resource://" + getPackageName() + "/" + R.raw.video2;
    view.setVideoURI(Uri.parse(path));
    mediaController.setAnchorView(view);
    view.setMediaController(mediaController);

    if(savedInstanceState!=null){
        int currentPos =  savedInstanceState.getInt("current position");
        view.seekTo(currentPos);
    }

    view.start();

}