MediaPlayer Example In Kotlin

MediaPlayer Example In Kotlin | Android Studio Example

Hi friends, today in this Kotlin Tutorial we will learn how to make an Android Media Player using Kotlin language and we will make use of MediaPlayer class in Android. So, lets start working on our MediaPlayer Example In Kotlin.

 

What is Android MediaPlayer Class:-

In Android Studio MediaPlayer class is a class that allows to access video or audio files using resources. We can access any audio/video file from our system . Also from a data stream over network.

The common methods of the MediaPlayer Class are:

  • getDuration()
  • getCurrentPosition()
  • setVolume()
  • pause()
  • isPlaying()

 

MediaPlayer Example in Kotlin:-

 

1- We start with a new Android Studio Project and name it as “KotlinMediaPlayer”. The name of the Android Project totally depends on your choice.

2- I have selected the API level to 19. Because, it will help you to run your application on almost 98% of the Android devices. But, if you want to select any higher level API you can change the API level from the list.

 

MediaPlayer Example In Kotlin

3- So after your project builds, you will have two files. “MainActivity.kt” file and “activity_main.xml” file. First we will design the UI of our Android Player in activity_main.xml file.

 

Media Player Example In Kotlin
Media Player Example In Kotlin

 

“activity_main.xml”

 

4- First change the default layout from Constrain Layout to Relative Layout. Then, in our activity_main file we need some widgets. TextView, ImageView, ImageButton and SeekBar. First we will use two TextViews at the top. One to display “Now Playing” and second one to display the name of that song/audio etc.

 

 

5- Below the name of the song/audio we will use an ImageView to set the background image of the Media Player. For doing this first come to res –> drawable and add the image in your drawable folder that you want to set as your background image. Use that image to set it in the background of your ImageView.

 

 

6- So, now we have to use 4 ImageButtons. With these ImageButtons we will handle the Play, Pause, Forward and Backward functions of the player.

 

 

7- We will add two TextViews to show the start time and end time of the audio file.

 

 

8- So, in the last we will add a SeekBar to handle the progress of the audio.

 

 

9- And the final activity_main.xml file will look like this.

 

 

“MainActivity.kt”

 

10- After working with our activity_main.xml file now we have to add Kotlin code in our MainActivity.kt file. First of all go to res folder and under res folder you will have a folder named raw. Add your audio file in this raw folder. If you don’t have a raw folder under your res folder then right click on res folder and select “Android Resource Directory”. Name the new directory as “raw“.

 

MediaPlayer Example In Kotlin

 

11- After adding your audio file in the raw folder now we will add some variables (var) and val to set the default start time and end time in seconds. We set forward and backward time as 5 seconds by default. Also we are going to set up a handler to handle the progress of the SeekBar.

 

 

12- So now in onCreate function we will display the title of the audio file. The name of the audio is “fatiha” on my case. Also we will disable the Pause button.

 

 

Setting up onClickListeners on ImageButtons:-

 

13- Now we have to set onClickListener for our ImageButton. On our Play button which has id (buttonPlay) we are going to set a few things. First we get the current start and end time of the MediaPlayer and convert it into Int (Integer Value). We define an if-else condition so that the MediaPlayer cannot move behind on start and cannot move ahead after end.

After that we will set the current position and the total duration of the audio file into our TextView. Handler will update the SeekBar on every 100 milliseconds and the Pause button will be enabled after the MediaPlayer starts playing the file.

 

 

14- After that now we set an onClickListener for our Pause button which has an id (buttonPause). We will display a Toast message “Audio Paused” whenever the audio will be paused by the user. Also we will enable the Play button and disable the Pause Button.

 

 

15- Now we apply an onClickListener on our Forward Button which has an id (butonForward). So, Forward Button will move the SeekBar 5 seconds ahead of the current position. But, at the end if the MediaPlayer is unable to move 5 seconds ahead it will display a Toast message “Cannot jump forward 5 seconds”.

 

 

16- After that now we add an onClickListener on our Back Button which has an id (backButton). Since, it will move the MediaPlayer 5 seconds behind from the current position but if it can’t move 5 seconds behind it will display a Toast message “Cannot jump backward 5 seconds”.

 

 

Handling The SeekBar:-

17- To handle the SeekBar according to the current position of the MediaPlayer we use function setOnSeekBarChangeListener(). Because SeekBar is responsible to move according to the MediaPlayer. seekbar is the id of our SeekBar in XML.

 

 

18- After handling our SeekBar now at the end we have to set a function setOnCompletionListener() on our MediaPlayer. Because of this MediaPlayer will start again from the beginning and the current duration will be updated as well.

 

 

19- Finally outside our onCreate method we will make a Runnable object to handle the current position of the SeekBar according to the MediaPlayer. This will update the current position of our SeekBar on every 100 milliseconds.

 

 

20- So, this is all for our MediaPlayer example in Kotlin. Our final MainActivity.kt file will look like this.

 

 

Finally, here we complete our tutorial on MediaPlayer Example in Kotlin. If you have any queries you can ask in the comments section below.

 

OUTPUT

 

Media Player Example In Kotlin
Media Player Example In Kotlin

 

Media Player Example In Kotlin
Media Player Example In Kotlin

 

Media Player Example In Kotlin
When Audio Paused. Media Player Example In Kotlin

 

Media Player Example In Kotlin
Android Media Player Example In Kotlin

 

Example Media Player In Kotlin

 

 

 More Tutorials On Android:-

Leave a Comment

Your email address will not be published. Required fields are marked *