Reputation: 75
I want to be able to add a video I created that automatically starts playing when it's brought up on the screen, so far my code looks like this:
VideoView videoview = (VideoView) findViewById(R.id.button10);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.group_project);
videoview.setVideoURI(uri);
videoview.start();
Currently nothing happens when I click and go on that screen. It plays sound when I used media player but I really want to add this video. any help would be much appreciated.
Upvotes: 3
Views: 8517
Reputation: 1113
Try this before doing videoview.start() to ensure is ready.
videoview.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
videoview.start();
}
});
Upvotes: 0
Reputation: 126563
Very important the use of setOnPreparedListener()
method, and be sure to have a video called group_project.mp4
inside /raw
folder.
final VideoView videoview = (VideoView) findViewById(R.id.button10);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.group_project);
videoview.setVideoURI(uri);
videoview.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoview.start();
}
});
Read the documentation:
A MediaPlayer object must first enter the Prepared state before playback can be started. There are two ways (synchronous vs. asynchronous) that the Prepared state can be reached: either a call to prepare() (synchronous) which transfers the object to the Prepared state once the method call returns, or a call to prepareAsync() (asynchronous) which first transfers the object to the Preparing state after the call returns (which occurs almost right way) while the internal player engine continues working on the rest of preparation work until the preparation work completes. When the preparation completes or when prepare() call returns, the internal player engine then calls a user supplied callback method, onPrepared() of the OnPreparedListener interface, if an OnPreparedListener is registered beforehand via setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener).
Upvotes: 7
Reputation: 1472
VideoView videoview = (VideoView) findViewById(R.id.button10);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.group_project);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoview.start();
Upvotes: 0