Kotlin — playing audio file

A simple example of playback

Dair Diaz Rambao
5 min readJun 12, 2018

Kotlin is a very exciting programming language, when I had my very first contact with it I really liked it. Today I want to give you a basic example of how to use Kotlin to play an audio file (mp3), using the MediaPlayer API.

I will try to maintain this reading as short as possible.

Get ready

I assume you have a basic knowledge of Android/Java/Kotlin so let’s begin. First, you have to create an empty project in Android Studio (I’m using version 3.1.1), and Kotlin 1.2.

This configuration isn’t a big deal so you can setup something like this:

Step 1: project name and location

Name it as you want, in my case I used AudioFile and put it in the location that you can remember.

Click next and define the other project’s characteristics like this:

Step 2: minimum API level
Step 3: empty activity

I just need one screen for this exercise, so, I choose Empty Activity.

Final step: initial activity and layout

Coding time

First, open the MainActivity.kt file and you’ll see a default template class using the Kotlin syntax.

The first thing you need to do is define a property of type MediaPlayer, this object is able to play/pause/seek the audio player. The final code should look like the image below:

Define an attribute to control de audio player

As you can notice, I defined the mediaPlayer property as optional and private, but feel free to use lateinit and public, it doesn’t really matter for this example.

After this, you’ll need to initialize the mediaPlayer object using the next code (line 14):

Initialize the mediaPlayer object

As you can see, there’s a beep_15sec (line 14) file which it’s full name is beep_15sec.mp3, this file is located inside a raw subfolder of res folder. So, if you don’t have this audio file yet, first, create the raw folder inside res and put the file in it.

Also you can see in line 15 to 17, there’s a listener method which is able to tell you when the audio is ready to playback, as API Doc indicates:

Register a callback to be invoked when the media source is ready for playback.

That callback isn’t relevant for this example, but it’s good to know what you can do with it.

Working in layout

I need an element on the UI to start the playback, because of that, I have to add a button and listen for the tap event, to do that, I need to open the activity_main.xml file and write some xml like this:

Button which starts the playback

The important part in the code above is line 11 to 25, which is our button, and as you can see, it has an identifier (line 12) “pushButton” which we’ll use in a sec.

Back to the code

As I said before, I need to listen for tap events, so we add a listener using the next code (line 21 to 24):

Tap listener

This way you can listen for tap events on the pushButton, but, where the heck did I define the pushButton property inside the MainActivity class? Well, that’s one characteristics of Kotlin which I like the most. In line 7, basically we import all the elements from the activity_main layout and automatically I can use them without the old findViewById method, this capability is only possible because of the Kotlin extension which in my case is already installed inside the project.

Kotlin extensions

But it’s something left, the method handleTouch, which does all the work, so, let’s see it:

handleTouch method

In fact, we are using a touch event listener instead of tap, so the MotionEvent object contains all the event-flow for this user action. Because of that, I need to identify just when the user presses down the button and plays it back as line 32 shows. So, to complete the feature, I need to stop the playback in some point, so I use the press up and press cancel event to do it (line 36). In line 37, I try to rewind the playback to start again when the user presses down the button.

Run

Final app

If you run the app, you’ll get a simple audio player app which was the goal. There’re a lot of details maybe you want to dive into, but it’s up to you.

This was just a brief step by step of using Kotlin and the MediaPlayer API, if you want to go deeper using MediaPlayer, please read all about the API reference and practice a lot.

--

--