在android中如何加载视频当前位置?

时间:2013-08-20 15:15:26

标签: android android-videoview

我有一个播放视频的简单视频。我试图让它顺利播放,而不是在从纵向到横向更改方向时重新启动或重新启动活动。这是视频代码:

        uriStr = ("http://video.com/video/" + yyyyMdd_Str + "/" + yyyyMdd_Str + ".mp4");

    testMediaCon1_MC = new MediaController(this);
    testMediaCon1_MC.setAnchorView(testVideo1_VV);
    testVideo1_VV.setMediaController(testMediaCon1_MC);
    testVideo1_VV.setKeepScreenOn(true);
    testVideo1_VV.setVideoPath(uriStr);
    testVideo1_VV.requestFocus();
    testVideo1_VV.start();

我认为在方向改变时使视频播放顺畅我必须得到视频的当前位置,然后在方向改变发生时加载当前位置。所以我有代码来获取视频位置:

    @Override
protected void onSaveInstanceState(Bundle out) {
 // TODO Auto-generated method stub
 super.onSaveInstanceState(out);
 if (testVideo1_VV.isPlaying()) out.putInt("pos", testVideo1_VV.getCurrentPosition());
}

但现在我无法弄清楚加载视频位置的代码是什么:

    @Override
protected void onRestoreInstanceState(Bundle in) {
 // TODO Auto-generated method stub
 super.onRestoreInstanceState(in);
 //load video position here
}

有什么想法吗?或者我完全错了吗?

2 个答案:

答案 0 :(得分:2)

您可以使用seekTo从保存的位置重新开始播放,但它无法解决重新缓存的问题 - 一旦您的VideoView被销毁,刷新缓冲区并且在重新开始播放之前,必须准备好媒体并再次缓冲。

如果您希望在更改方向后继续无缝播放,则必须保持活动并自行处理更改 - 请参阅http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

答案 1 :(得分:0)

我想出的解决方案是,在改变方向时全屏播放视频不会像我想象的那样覆盖onRestoreInstanceState,而是覆盖onConfigurationChanged。在清单中,应该调用android:configChanges="orientation",然后覆盖onConfigurationChanged,这就是我如何让它为我工作:

    @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

        buttonsLL.setVisibility(View.GONE);
        bufferLL.setVisibility(View.GONE);
        listLL.setVisibility(View.GONE);

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) videoSA_VV
                .getLayoutParams();
        lp.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
        videoSA_VV.setLayoutParams(lp);

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

        buttonsLL.setVisibility(View.VISIBLE);
        bufferLL.setVisibility(View.VISIBLE);
        listLL.setVisibility(View.VISIBLE);

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) videoSA_VV
                .getLayoutParams();
        lp.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
        videoSA_VV.setLayoutParams(lp);
    }

}

关键是要设置您不想显示的视图的可见性为GONE,然后在方向变回时将它们设置回VISIBLE。将视图设置为GONE后,您可以拉伸VideoView以填充父视图。