public class video extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView = (VideoView) findViewById(R.id.surface);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.wildlife);
Intent tostart = new Intent(Intent.ACTION_VIEW);
startActivity(tostart);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.start();
}
}
这是播放视频的代码。但是视频停留在我的屏幕上并且无法播放。有没有解决方案?
答案 0 :(得分:2)
video_player_view.xml
<?xml version="1.0" encoding="utf-8"?>
<VideoView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/VideoPlayerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Java code:
public class VideoPlayerActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player_view);
String video_file_path = ?;//give the path of your video
VideoView video_view = (VideoView) this.findViewById(R.id.VideoPlayerView);
MediaController mc = new MediaController(this);
video_view.setMediaController(mc);
video_view.setVideoPath(video_file_path);
video_view.requestFocus();
// start video
video_view.start();
video_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
endActivity();
}
});
}
public void endActivity() {
this.finish();
}
}
答案 1 :(得分:1)
您为什么使用此代码?
Intent tostart = new Intent(Intent.ACTION_VIEW);
startActivity(tostart);
如果没有必要那么将其删除视频将播放声音。否则,请写下你想要做的视频播放。