0

我正在尝试在 android 应用程序中播放来自网站的视频。我要播放视频,但有两件事我担心:

  1. 当我在播放视频时旋转手机时,它会重新加载视频并重新从头开始。

  2. 视频结束后,它似乎仍在做某事,我无法点击后退按钮并返回上一个活动。

Java 文件:

public class IntroVideo extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.intro_video);
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    try {

        String link = "http://northeastvision.org/vids/tube_rush.mp4";
        final VideoView video = (VideoView) findViewById(R.id.videoDisplay);
        final ProgressDialog mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Loading video Please wait...");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();


        video.setMediaController(new MediaController(IntroVideo.this));
        Uri uri = Uri.parse(link);
        video.setVideoURI(uri);

        video.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {

                video.start();
                mProgressDialog.dismiss();
            }
            });

    }
    catch (Exception e) {
        Toast.makeText(this, "Error Connecting!", Toast.LENGTH_SHORT).show();
    }
    }

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

<VideoView
        android:id="@+id/videoDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</RelativeLayout>
4

2 回答 2

0

您可以做的一件事(这将为您做两件事:结束并返回上一个屏幕)将是:

video.setOnCompletionListener (new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {

            mp.release();
            this.finish();//kills current Activity
        }
 });
于 2013-04-30T23:18:28.747 回答
0

1) 为防止在旋转手机时重新加载,将属性 android:configChanges="orientation|screenSize 添加到您的 Manifest.xml

 <activity android:name="IntroVideo"
                android:configChanges="orientation|screenSize" />

2)释放与MediaPlayer相关的资源并完成活动。

 video.setOnCompletionListener (new OnCompletionListener() { 
        @Override 
         public void onCompletion(MediaPlayer mp) { 
         video.release() 
         this.finish(); 
          } 
    });

阅读更多关于:

android:configChangesMediaPLayer.release():

于 2014-01-09T04:21:02.230 回答