Tag Archive for notification

Notification buttons

Notification buttons

This tutorial shows how to make an audio playback service and how to control audio playback using buttons on a notification. I am going to assume that you have some knowledge of android app development, and know your way around Eclipse, or other SDK used for Android. This is my first tutorial, so please let me know in comments if anything needs to be explained in more depth, or point out any mistakes made.

1. Create service

First things first, we need to create the service that will play music. You can do that by creating a new class that extends Service. You will also need an instance of MediaPlayer. Your service should also implement OnPreparedListener and OnCompletionListener interfaces to receive callbacks from the MediaPlayer. Your code should look similar to this:

onCreate() is called when your service is first started. We’ll use it to instantiate the MediaPlayer, and set listeners. onStartCommand() is called every time you launch your service from an activity – this could happen several times during service’s lifetime. Returning START_STICKY causes your service to stay alive after onStartCommand() finished executing.
You should also define your service in your project’s manifest. Put the following between the <application> tags:

2. Let’s play some music

Now that we have the service, let’s make it play the music. We will modify onStartCommand() to handle the incoming intent, and override onDestroy() to stop the music when service is stopped:

This will get the url from intent and use it as a data source for media player. You can use this to stream media from the internet. the prepareAsync() method starts buffering the audio stream in a secondary thread, and will call onPrepared() when finished – that’s where you start playback by calling start() on the MediaPlayer.

If you want to stream music from the internet, don’t forget to request the INTERNET permission inside the <manifest> tags of your manifest:

Next we’ll create an Activity (you should know how to do this) that will start the service. Add 2 buttons the layout: first button will start the service and play music, while the other one will stop the service:

The code above will start the service with url link provided. It should start streaming Radio Nova. The app should work at this stage. Go on and try it!

3. Show the notification

The final part of this tutorial is creating the notification that will show when service is running and present user with playback control buttons. Let’s create a showNotification() method inside our Service. The notification will have Stop and Pause Buttons. You can add buttons by addAction(). We should also define the broadcasts for those buttons.

We will use NotificationCompat class from the support library for building the notification so it’s backwards compatible. If you want you can use the Notification class, however it’s not recommended.

To make my life easier, I made the createPendingIntent() method to create broadcast pending intents easy:

startForeground() method will bring your service to the foreground by showing the notification.

Notification should be shown whenever new stream is played:

Now all that’s left is handling the button presses. To do that, we need to create a BroadcastReceiver, register it inside onCreate() method, and unregister when service is destroyed:

At last, we should implement the onCompletion() method. It is called when song finished playing. In my implementation it simply stops the service:

That’s it! You can now test the app. When you start the audio, you should be able to see this in your notification shade:

Notification buttons

Of course, the app is a bit rough around the edges; the “pause” button doesn’t turn into “play” when you press it, but I will leave this as an exercise for the reader.

Full source code: