How to display video files on Android systems using VideoView
13 minute(s) read
Published on: May 03, 2021
Updated on: Dec 14, 2021
Videos can be found in files such as source files, is files that are traced to raw folders or subfolders of these folders, video files that are placed on a memory card and used for display, such as files that can be used to display online, which can also be accessed via URL. So, in this tutorial, we want to tell you how to display different types of recorded files on Android systems. It is recommended that you follow this tutorial.
Introducing VideoView on Android
- SetVideoUri () and setVideoPath () methods:
It should be noted that these methods are used to specify the recorded address.
- Pause () method:
This method is also used to stop the recorded that is playing.
- CanPause () method:
It should be noted that this method is also used to determine whether or not the recorded can be stopped. In other words, this method is used to check whether the playing recorded can be stopped or not. The output and result of this method are true or false, which is the result of checking whether the playing recorded can be stopped or not.
- SeekTo (int millisecond) method:
Using this method, you can specify which part and which second the recorded will start playing. In other words, using this method, we specify from which part the recorded will start playing.
- Resume () method:
We also use this method to start playing the recorded we stopped before.
- StopPlayback () method:
This method also issues a command to stop the recorded. The main command issued to pause the recorded is done by this method.
- CanSeekForward () method:
It should be noted that this method also checks whether the recorded can jump forward. In other words, this method is used to check if the videos can be thrown forward. The result of this method is true or false, which will tell whether it is possible to jump forward in this recorded or not.
- CanSeekBackward () method:
This method also checks whether the recorded can go back. This method will display the true and false results because it checks whether the recorded can go back or not. If it is not possible to go backward, it will show false, and if it is possible to go backward, it will show true as output.
- GetDuration () method:
This method is also used to get the duration of the recorded. In other words, the output of this method will be the recorded duration.
- IsPlaying () method:
This method will also check if the recorded is playing. Like other methods, the output of this method will be true or false because it checks whether the recorded is playing or not, and if the recorded is playing, it will show the output is true and otherwise false.
- SetOnPreparedListener method (MediaPlayer.OnPreparedListener):
This method has many uses and can be used in different places, and some settings that we want to give to videos can be done using this method.
- SetOnErrorListener method (MediaPlayer.OnErrorListener):
This method is called when something goes wrong while playing recorded. In other words, this method is used to solve problems that occur when playing videos.
- EtOnCompletionListener method (MediaPlayer.OnCompletionListener):
This method, if defined, will be called after the end of the video. To make all the settings to be done after the whole video.
Note that VideoView is responsible for displaying the video and can do nothing else. In other words, you cannot use VideoView if you want to adjust the settings for the video being played. For example, it is impossible to rewind or advance the video being played or make other custom settings in the video being played using VideoView, which is why MediaController is used to set and apply custom settings for the video being played.
1- In Android Studio, we create a new project and choose its name as desired. For example, the name of this project in this tutorial is VideoView.
2- The activity needed in this project and this training is an activity of the Empty Activity type. Select an activity of Empty Activity type.
3- It should be noted that the language required for this project is Java. Select the Java language.
4- Add the VideoView widget to the Activity Layout.
5- The codes that should be in the activity_main.xml section are as follows:
Offline and offline this files can also be used, and files that can be used offline to display are in the resources folder or on the device memory card.
6- We put a video file with an MP4 extension in the raw folder of the project, which is for displaying video and video files.
7- Then, we complete an activity like the following.
8- The codes that should be in the MainActivity.java section are as follows:
In the inserted codes, we defined the VideoView redball of the type called myVideoView.
To determine the location of the video file, we use the setVideoURI () method.
9- Define a URI file and name it videoURI and select the desired video file:
The URL is used to specify the address and location of the video file. The getPackageName () method returns and obtains the desired package name.
10- We will implement the project.
11- After playing the video, we will see that the video is played, but we cannot control the video, and we cannot control and manage it.
12- To manage and control the video, we create an instance of the MediaController class with the desired and selected name mController.
13- We have to connect myVideoView to the controller using the setMediaController method. Like the following:
14- We will implement the project.
15- After running this project, we will see that the video is playing and if we click on the video being played, control buttons will appear that we can use to manage and control the video.
16- The control buttons displayed on the video for management include buttons for rewind, fast forward, stop, and a seekBar that displays the position and time of video playback.
17- Another problem we have in this part of the tutorial is that the seekBar is very bottom. We have to provide settings that receive the length and width of the video and display the seekBar accordingly to indicate Video status is used.
18- To do this, use setAnchorView ().
19- We also use the setOnPreparedListener () method to get the video length and width.
20- Add the setOnPreparedListener method to the activity. Like the following:
21- Inside this method, we define another method, such as setOnVideoSizeChangedListener until the length and width of the video are specified.
22- Then, we define the setAnchorView () method inside the same method:
23- The complete and final code that should be in the MainActivity.java file are as follows:
24- We will implement the project.
25- After executing the project, we will see that the tape is placed on the video and is not far from the video. Rather, it is completely on the video.
26- MediaController has other methods for control and management.
This method is used to display the bar so that if we call this method, we do not need to click on the video again to display the bar. The tape is displayed automatically without clicking on the video.
show :(int timeout)
This can also be used to perform activities similar to the previous method, but the difference that was using this method can specify the duration of the bar display. The display time of the bar can be specified in milliseconds:
In the code inserted above, we display the bar for three seconds, and after three seconds, the bar is hidden and is not displayed. After hiding, it should be noted that if we click on the video again, it will reappear and be displayed for three seconds and then hidden.
This method is used to hide the controller bar.
is Showing ():
This also checks whether the bar is being displayed and takes some action based on it.
A message is displayed after we run the project, and the video is displayed and then completed.
In this part of the tutorial, I use a listener to get an error when playing a recorded, which is called SetOnErrorListener.
It should be noted that if the user changes the position and direction of the phone while playing recorded, for example, from landscape to portrait mode or vice versa, he cannot save its mode and therefore plays the recorded again from the beginning.
In this case, if the user exits the activity or changes the position and direction of the screen from horizontal to vertical or vice versa, the recorded will be displayed from the beginning. That's why we need to provide settings that display the recorded from where it left off and not go to the beginning of the recorded.
For this we use the savedInstanceState () and onRestoreInstanceState () methods.
The savedInstanceState is used to store the recorded being displayed, and the onRestoreInstanceState is used to retrieve data.
Inside the onSaveInstanceState, we save the latest status of the recorded being played.
To do this, we use the putInt (), which also has two parameters. The first parameter is the key used when retrieving data, and the second parameter is an int that we get the VideoView component using getCurrentPosition.
The recorded position is captured and saved in milliseconds.
Then we take the value saved by putInt using the MyVideoPosition key from within the onRestoreInstanceState using getInt and store it in video position.
27- We will implement the project.
28- After playing the recorded, you will see that if we exit the activity or change its direction and return to the recorded display, the video will be played from where it was. It does not go back to the beginning of the recorded.
As mentioned, the two methods onSaveInstanceState () and onRestoreInstanceState () are used at different times and can perform some tasks. Such as when the recorded starts to show when it changes direction or exits the video from where it was before, not to go to the beginning of the recorded. But suppose we want to provide settings that do not restart when the recorded screen is playing. In that case, we can make our desired settings without using these methods and using attribute properties inside the activity tags that are in the project manifest.
So, without using this and using the features inside the project manifest tags, we can apply settings displayed by rotating the recorded screen from where it left off, not from the beginning.
The code that should be in the AndroidManifest.xml section is as follows:
In the code above, inside the activity tag related to MainActivity, we defined a property called configChanges with the orientation value. It should be noted that these features that we defined have a higher priority than the methods that we defined for the project above. This means that the priority and importance of these features defined and inserted inside the tags related to the project manifest are more than the methods we defined above.
In the code above, if configChanges are defined with an orientations value and the save and restore methods in an activity simultaneously, it will prevent the activity from restarting when the screen rotates and changes direction.
The volume of videos added to the project is very important for launching and executing them, so we must be careful in choosing videos to be placed in projects so that the final volume of the project is not more. In other words, we can even use methods that use online videos available on the Internet. In this case, the final volume of the project will not be large, and it can be easily implemented without any problems.
Online video playback on VideoView on Android systems
It should be noted that to play videos online, we must have a license to access the Internet so that we can easily use the videos available on the Internet.
29- We add the INTERNET license to the project manifest.
30- The codes that should be in the AndroidManifest.xml section are as follows:
Then we need to add the file address on the server to VideoView instead of the local file address, using the setVideoURI or setVideoPath methods.
31- The code that should be in the MainActivity.java section is as follows:
In this part of the tutorial, we learned how to display recorded files and add control buttons for more management and control.
We also applied how to display and change the screen orientation and related settings. After exiting the activity or changing the screen orientation, the recorded will be displayed from where it was, not from the beginning.About our Android and iOS app development services