Download project here:
Download project here:
https://codeible.com/coursefiles/androidvideoview
Hello, in this tutorial, I will show you how to use the VideoView to display video.
Playing the video
To play a video using a file in the project, create a new Android Resource Directory call raw for our video files. Then drag and drop a video into the raw directory that you want to display.
String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.intro;
Create a Uniform Resource ID or Uri with the path and call setVideoUri() from the VideoView object to add the video to it.
Now that the video is added to the VideoView, take the VideoView object and call the start() method to play the video.
videoView.start();
If we run the app and click on the button, the video will start.
Displaying a video on the device
Now let’s see how we can play a video from the device. Begin by dragging a video file into the emulator so there will be a file for us to work with.
In Android Studio, register a file chooser intent for the app using registerForActivityResult() inside the onCreate() method, before the setOnClickListener code.
Grab the videoView and call setVideoUri to add the video. Then call start() to play the video.
Launching the File Chooser Intent
Now that we registered a file chooser intent on our app, we want to launch it when we click on the button.
Remove the code inside the onClick() method, then grab the fileChooser launcher, and call the launch() method.
If we run the app and click on the button, it’ll bring us to the file chooser activity. If we select the video, it’ll bring us back to the app and play it on the VideoView.
Playing an Online Video
Now let’s see how we can display an online video.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Go to the MainActivity file and delete the code inside the onClick() method.
If we run the app and click on the button, it’ll play the video that was retrieved online.