MediaPlayer: Simplified Video Playback on Android
Playing videos is a common requirement in Android apps. In this tutorial learn about handling video playback via the MediaPlayer API on Android. By Bhavesh Misri.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
MediaPlayer: Simplified Video Playback on Android
25 mins
- Getting Started
- Understanding the code
- MediaPlayer
- Playing Video From Local Resources
- MediaPlayer is prepared
- Improving the UX
- Interacting with the SeekBar
- Playing Video from the Gallery
- Playing the video after it has been downloaded
- Releasing the Resources
- Executing Asynchronously
- Streaming a Video From a URL
- Digital Right Management
- DRM MediaPlayer
- Where To Go From Here?
Playing audio or video in Android apps is a common requirement for many projects. Many apps in the Google Play Store, even some non-streaming ones, provide audio and video playback. Above all, it’s an important topic that will lead you to many job opportunities.
In this tutorial, you’ll build an Android app that plays video from various sources, such as videos locally stored in your phone, the res folder, gallery and a URL using MediaPlayer
.
Along the way you’ll learn about:
- MediaPlayer.
- The states of MediaPlayer.
- Playing video from the res/raw folder.
- Playing video from a URL.
- Best practices for MediaPlayer.
- Digital Right Management.
Getting Started
Download the materials using the Download Materials button at the top or the bottom of this page. Extract and open the starter project in Android Studio.
Build and run. You’ll see something like this:
The app isn’t interactive yet because the starter project only consists of UI and some basic code. You’ll implement the functionality throughout this tutorial.
Understanding the code
Before the hands-on part of this tutorial, take some time to understand the codebase you’ll build on. Navigate to these three files and check out their contents:
The class implements a few interface classes to manage MediaPlayer
and seekBar
callbacks. You’ll understand these implementations once you start working on the functionality.
Also, to keep your app from falling asleep, you’ll need to keep your screen on. Notice this line inside onCreate()
which adds a flag window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
. Setting this flag ensures your screen stays on, while the app is in use. You don’t want the user’s device to fall asleep and lock while they’re watching a video on your app, do you?
- AndroidManifest.xml: At the top of the manifest file you’ll find
android.permission.INTERNET
. In this tutorial, you’ll play a video from a URL, so you’ll need the INTERNET permission. -
activity_video.xml: This is the one and only layout file in the project. It consists of:
- A
VideoView
to play video. - A
ProgressBar
to shows the user it’s loading a video. - Two
TextViews
and aSeekBar
to show progress. - An
ImageButton
to play and pause a video.
- A
-
VideoActivity.kt: This might look a bit overwhelming, but if you skim through and read the comments, you’ll find it quite simple.
The class implements a few interface classes to manage
MediaPlayer
andseekBar
callbacks. You’ll understand these implementations once you start working on the functionality.Also, to keep your app from falling asleep, you’ll need to keep your screen on. Notice this line inside
onCreate()
which adds a flagwindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
. Setting this flag ensures your screen stays on, while the app is in use. You don’t want the user’s device to fall asleep and lock while they’re watching a video on your app, do you?
MediaPlayer
MediaPlayer
is a part of the Android multimedia framework that plays audio or video from the resource directory and gallery. It also streams music or video from a URL.
When a media file plays, the MediaPlayer
API goes through several states, which are summarized in the diagram below:
That’s a lot of information to take in, but before you can use the
At this stage, you can’t play, pause or stop the media. If you try to force it, the app might crash.
-
Idle State:
MediaPlayer
is in an idle state when you first instantiate it, or first create it using the new keyword. You also reach this state after you callreset()
.At this stage, you can’t play, pause or stop the media. If you try to force it, the app might crash.
-
End State: Calling
MediaPlayer
‘srelease()
method frees resources and moves it to the end state. At this stage, you can’t play or pause the media. -
Error State: You reach this state if you try to play, pause or stop an uninstantiated
MediaPlayer
object. However, you can catch the error using MediaPlayer’sonErrorListener.onError()
callback. -
Initialized State:
MediaPlayer
reaches this state when you set a data source. To set a one, usesetDataSource()
method. Be aware that you can only set a data source whenMediaPlayer
is in an idle state or it’ll throw an IllegalStateException. -
Prepared State: Before you play any media from a file or a URL, you need to prepare your
MediaPlayer
by calling eitherprepare()
orprepareAsync()
. Once prepared, it reaches this state and callsonPreparedListener()
. -
Started State: Once
MediaPlayer
is ready, you can play the media by callingstart()
. While the music or video plays,MediaPlayer
is in a started state. -
Paused State: When you pause the media,
MediaPlayer
is in this state. To pauseMediaPlayer
, callpause()
. -
Stopped State:
MediaPlayer
is in this state when media stops playing. IfMediaPlayer
is in this state and you want to play the media again, you have to prepare it again by callingprepare()
orprepareAsync()
. -
PlaybackCompleted State: When the playback is complete,
MediaPlayer
is in this state. Additionally, invokesonCompletion.onCompletion()
. IfMediaPlayer
is in this state you can call thestart()
and play audio or video again.
Okay, enough theory. It’s time to code!
Playing Video From Local Resources
You will start with playing a video file in the raw directory. There’s a video in the starter project named test_video.mp4.
From the states you learned above, you may see that to play the video in your VideoView
, you have to:
- Set the data source using
MediaPlayer
- Call the
start()
function
But you may be wondering where do you add this code? For this, a couple of options may come to your mind.
The most common way to add new code into Android apps is to put it in the onCreate()
method, because you know it will be the entry point of your activity. But in the case of Media Player
that’s not good practice for two reasons:
- First, you don’t know the size of the video or how much time
MediaPlayer
will take to load it. - Second,
MediaPlayer
can play a video inVideoView
through its surface, which also takes time.
Take a look at the code inside the onCreate()
method of VideoActivity. In there you will see the following line: video_view.holder.addCallback(this)
, this gets a callback when the VideoView
‘s surface is ready for playing video. When this surface is ready, it calls surfaceCreated()
.
Next, inside surfaceCreated()
replace // TODO (1) with:
mediaPlayer.apply {
setDataSource(applicationContext,
// 1
Uri.parse("android.resource://$packageName/raw/test_video"))
// 2
setDisplay(surfaceHolder)
// 3
prepareAsync()
}
In this code, you perform three tasks:
- First, you pass the location, URI, of the video.
- Then, you set
MediaPlayer
‘s display to theVideoView
‘s surface by callingsetDisplay(surfaceHolder)
.Note: If you started theMediaPlayer
without calling this function, you wouldn’t see any video becauseMediaPlayer
wouldn’t know where to display the video. - Finally, you call
prepare()
. This function preparesMediaPlayer
to playback the video synchronously on the main thread.
MediaPlayer
without calling this function, you wouldn’t see any video because MediaPlayer
wouldn’t know where to display the video.