Android VideoView can be used to play videos in android application. Android VideoView plays videos from sources such as android resource, content provider or from server. However, it supports certain formats of vides only. You can find Android platform supported media formats here.
VideoView provides methods such as setVideoURI, setVideoPath, start, stopPlayback, pause and resume methods to handle media.
In this tutorial, you can learn how to use VideoView with an example, about various listeners such as OnCompletionListener, OnPreparedListener, OnInfoListener, and OnErrorListener, using VideoView with MediaController and playing next video automatically.
To play a video in your android application, you need to first add VideoView element to your screen layout xml. Then in your activity, get the VideView object, set a video file to be played by calling setVideoURI on VideoView object passing video URI and then start the video by calling start method.
The example shows how to play a video file which is saved in raw resource folder and uses start and stopPlayback to start and stop playing videos.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_margin="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"></VideoView>
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
style="@style/Widget.AppCompat.Button.Colored"
android:text="Play"
android:onClick="start"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/pause"
app:layout_constraintTop_toBottomOf="@+id/videoView"/>
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
style="@style/Widget.AppCompat.Button.Colored"
android:text="Pause"
android:onClick="stop"
app:layout_constraintLeft_toRightOf="@+id/play"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/videoView"/>
</android.support.constraint.ConstraintLayout>
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.VideoView;
public class VideoActivity extends AppCompatActivity {
private VideoView vv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_activity);
vv = findViewById(R.id.videoView);
Uri videoUri =
Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.imageswitcher_example);
vv.setVideoURI(videoUri);
}
public void start(View view){
vv.start();
}
public void stop(View view){
vv.stopPlayback();
}
}
You can play in VideoView videos from server or website as shown below.
VideoView vv = findViewById(R.id.videoView);
Uri videoUri =
Uri.parse("http://www.swebsiteaaa.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4");
vv.setVideoURI(videoUri);
vv.start();
To play a video that exists on the device or external storage, first you need to make sure that required permissions have been added to manifest file, then add file path to VideoView as shown below.
VideoView vv = findViewById(R.id.videoView);
vv.setVideoPath("/MyVideo/test.mp4");
vv.start();
You can add OnCompletionListener to VideoView by calling setOnCompletionListener method. OnCompletionListener has onCompletion callback method which gets called every time after VideoView completes playing a video.
To automatically play a list of video files one after another in VideoView, first define a counter and prepare a list of video files to be played in onCreate method of activity. In onCompletion method of listener, using the counter get the next video file from the list, add it to the VideoView and start the video as shown in the code below.
vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
if(!(currentPosition < videosList.size())){
return;
}
Uri nextUri =
Uri.parse(videosList.get(currentPosition++));
vv.setVideoURI(nextUri);
vv.start();
}
});
Like OnCompletionListener explained above, there are OnPreparedListener, OnInfoListener and OnErrorListener listeners. MediaPlayer.OnPreparedListener has one callback method onPrepared which gets called when media file is loaded and ready to play.
MediaPlayer.OnInfoListener has one callback method onInfo which gets called to communicate warnings or information about media and playback. The callback method onInfo returns boolean to indicate whether info has been handled in the method or not, true means handled.
MediaPlayer.OnErrorListener has one callback method onError which gets called to communicate error information. The callback method onError returns boolean to indicate whether error has been handled in the method or not, true means handled.
vv.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
return false;
}
});
vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
//do something when video is ready to play
}
});
vv.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) {
return false;
}
});
Android VideoView can be used with MediaController. MediaController provides controls such as paly, pause, fast forward and rewind and it also displays progress of the media being played. MediaController takes care of making the controls work for the media being played in VideoView.
To use MediaController, you need to first instantiate it, then set anchor view for it by calling setAnchorView method and add the media controller instance to video view by calling setMediaController method.
MediaController window appears near anchor view. Initially media controller is shown and will become invisible after 3 seconds of idle time. It will become visible when the anchor view is touched.
Below code shows how to use MediaController with VideoView.
VideoView vv = findViewById(R.id.videoView);
vv.setVideoPath("/MyVideo/test.mp4");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(vv);
vv.setMediaController(mediaController);
vv.start();
You can show and hide media controller programmatically in response to UI events by calling show and hide methods on media controller object.
mediaController.show(5000);
if(mediaController.isShowing()){
mediaController.hide();
}
You can make MediaController display next and previous buttons by setting next and previous listeners by calling setPrevNextListeners and providing two View.OnClickListener objects to handle next and previous button click events. In the listener’s callback method, you can set video path of VideoView to next or previous file.
mediaController.setPrevNextListeners(onClickListenerNext, onClickListenerPrevious);
private View.OnClickListener onClickListenerNext = new View.OnClickListener() {
@Override
public void onClick(View view) {
vv.setVideoURI(nextUri);
vv.start();
}
};
private View.OnClickListener onClickListenerPrevious = new View.OnClickListener() {
@Override
public void onClick(View view) {
vv.setVideoURI(previousUri);
vv.start();
}
};