VideoView is a UI widget that is used to display video content to the users within android applications. We can add video in this video view from different resources such as a video stored on the user device, or a video from a server. In this article, we will take a look at How to use Video View in the android application. A sample video is given below to get an idea about what we are going to do in this article.
<!-- adding VideoView to the layout -->
<VideoView
android:id="@+id/idVideoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idTVHeading"
android:layout_centerInParent="true" />
// Java
// on below line we are creating variables.
private VideoView videoView;
// Your Video URL
String videoUrl = "Paste Your Video URL Here";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are initializing our variables.
videoView = findViewById(R.id.idVideoView);
// Uri object to refer the
// resource from the videoUrl
Uri uri = Uri.parse(videoUrl);
// sets the resource from the
// videoUrl to the videoView
videoView.setVideoURI(uri);
// creating object of
// media controller class
MediaController mediaController = new MediaController(this);
// sets the anchor view
// anchor view for the videoView
mediaController.setAnchorView(videoView);
// sets the media player to the videoView
mediaController.setMediaPlayer(videoView);
// sets the media controller to the videoView
videoView.setMediaController(mediaController);
// starts the video
videoView.start();
}
// Kotlin
// on below line we are creating a variable.
lateinit var videoView: VideoView
val videoUrl = "Paste Your Video URL Here"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// on below line we are initializing our variables.
videoView = findViewById(R.id.idVideoView)
// Uri object to refer the
// resource from the videoUrl
val uri = Uri.parse(videoUrl)
// sets the resource from the
// videoUrl to the videoView
videoView.setVideoURI(uri)
// creating object of
// media controller class
val mediaController = MediaController(this)
// sets the anchor view
// anchor view for the videoView
mediaController.setAnchorView(videoView)
// sets the media player to the videoView
mediaController.setMediaPlayer(videoView)
// sets the media controller to the videoView
videoView.setMediaController(mediaController);
// starts the video
videoView.start();
}