我正在尝试在 android 应用程序中播放来自网站的视频。我要播放视频,但有两件事我担心:
当我在播放视频时旋转手机时,它会重新加载视频并重新从头开始。
视频结束后,它似乎仍在做某事,我无法点击后退按钮并返回上一个活动。
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>