如果你想让操作系统使用默认播放器播放视频,你会使用这样的意图:
String videoUrl = "insert url to video here";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(videoUrl));
startActivity(i);
然而,如果你想自己创建一个视图和流视频,一种方法是在你的布局中创建一个视频视图,并使用mediaplayer流视频到它。以下是xml中的videoview:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
然后在你的活动的onCreate中找到视图并启动媒体播放器。
VideoView videoView = (VideoView)findViewById(R.id.your_video_view);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
String str = "the url to your video";
Uri uri = Uri.parse(str);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
查看视频观看监听器,以便在视频播放完成或发生错误时通知您(VideoView.setOnCompletionListener,VideoView.setOnErrorListener等)。