We can play MP4 (MPEG-4), AVC, 3GP formats. We have a ready component that just sticks to somewhere on the activity. The video itself may already be in the application, but may also be located on an SD card. In this example, I will run the video file in the application. In the “res” directory of the application add a “raw” subdirectory. I upload a video file to it. The video I uploaded is a 2 and a half minutes video of dancing and singing parables. Do not upload too large a video file because every time you update the code and restart the program, the whole thing will be uploaded to the emulator or the phone. If the file was large, it would have been long-lasting.

 

In the next step, I stick to the VideoView component:

 

Then I make some changes to the main activity onCreate method:

 

Line 15 to link references to the VideoView component. I need to do this in order to be able to refer to this element in any way (eg pointing to a video file). Since I want to specify a video file contained in the application itself, I have to create a reference to it in the form of a Uri object and pass it to the setVideoURI method of the VideoView component (lines 16 and 17). On average it’s comfortable in my opinion. Too bad there is no way to simply pass the path as String. I have to specify the path to the file with the prefix “android: resource: //”, the package containing the activity, the raw directory, and the name of the video file without the extension.

 

On line 18 you can see the commented alternative. I’m giving the path to a video file outside the application using the setVideoPath method. Here for a change, we can give the path as a regular String.

Once launched, everything works as expected. The only slight comment – the video does not fit the video size set and is displayed in its original aspect ratio. Therefore, in the portrait orientation of me (Samsung ACE 3) when propping the 480: 800 screen, the video occupies 1/3 of the screen.