MediaPlayer Class
in Android is used to play media files. Those are Audio and Video files. It can also be used to play audio or video streams over the network. So in this article, the things discussed are:
MediaPlayer State diagram
Creating a simple audio player using MediaPlayer API. Have a look at the following image. Note that we are going to implement this project using the
Kotlin
language.
State Diagram of the MediaPlayer Class
The playing of the audio or video file using MediaPlayer is done using a state machine.
The following image is the MediaPlayer state diagram.
In the above MediaPlayer state diagram, the oval shape represents the state of the MediaPlayer instance resides in.
There are two types of arcs showing in the state diagram. One with the single arrowhead represents the synchronous method calls of the MediaPlayer instance and one with the double arrowhead represents the asynchronous calls.
The release method which is one of the important element in the MediaPlayer API. This helps in releasing the Memory resources allocated for the Mediaplayer instance when it is not needed anymore. Refer to
How to Clear or Release Audio Resources in Android?
to know how the memory allocated by the Mediaplayer can be released. So that the memory management is done accordingly.
If the stop() method is called using Mediaplayer instance, then it needs to prepared for the next playback.
The MediaPlayer can be moved to the specific time position using
seekTo()
method so that the MediaPlayer instance can continue playing the Audio or Video playback from that specified position.
The focus of the audio playback should be managed accordingly using the AudioManager service which is discussed in the article
How to Manage Audio Focus in Android?
.
The following image is the summarised version of the MediaPlayer state diagram.
Steps to create a simple MediaPlayer in Android
Step 1: Create an empty activity project
Create an empty activity Android Studio project. And select
Kotlin
as a programming language.
The MediaPlayer instance needs the attributes needs to be set before playing any audio or video file.
Invoke the following inside the MainActivity.kt file. Comments are added for better understanding.
Kotlin
importandroid.media.MediaPlayerimportandroidx.appcompat.app.AppCompatActivityimportandroid.os.Bundleimportandroid.widget.ButtonclassMainActivity:AppCompatActivity(){overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)// create an instance of mediplayer for audio playbackvalmediaPlayer:MediaPlayer=MediaPlayer.create(applicationContext,R.raw.music)// register all the buttons using their appropriate IDsvalbPlay:Button=findViewById(R.id.playButton)valbPause:Button=findViewById(R.id.pauseButton)valbStop:Button=findViewById(R.id.stopButton)// handle the start button to// start the audio playbackbPlay.setOnClickListener{// start method is used to start// playing the audio filemediaPlayer.start()// handle the pause button to put the// MediaPlayer instance at the Pause statebPause.setOnClickListener{// pause() method can be used to // pause the mediaplyer instancemediaPlayer.pause()// handle the stop button to stop playing // and prepare the mediaplayer instance // for the next instance of playbStop.setOnClickListener{// stop() method is used to completely // stop playing the mediaplayer instancemediaPlayer.stop()// after stopping the mediaplayer instance// it is again need to be prepared// for the next instance of playbackmediaPlayer.prepare()
8 min read
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy
&
Privacy Policy
Got It !