James Montemagno January 16, 2014 ,

Streaming audio in a background service with Xamarin.Android allows developers to create amazing user experiences. In my last post I walked though how to setup the a basic streaming service with a notification that your users could interact with. In Ice Cream Sandwich (API 14) Google introduced the RemoteControlClient, which exposes properties and methods be consumed by remote controls capable of displaying metadata, artwork and media transport control buttons. One of these “remote controls” is built into the lock screen of devices.


Since we already have a base streaming music service in place it is actually quite easy to take advantage of the RemoteControlClient to add lock screen music controls to our application.

A RemoteControlClient communicates with our application through Intents, specifically the Intent.ActionMediaButton which will be broadcast when our users press one of the buttons on the lock screen. We will first want to create a new BroadcastReceiver to handle any of these broadcasts to process them and pass the correct command to our background service. Here is what our BroadcastReciver looks like to handle these button presses:

Once we have our BroadcastReceiver in place, back in our service we are going to create three new methods. The first is to create and register the RemoteControlClient, second is to unregister it, and the last is to update the metadata for the lock screen to display.

First up is our register method, which will register our BroadcastReceiver with the AudioManager to allow it to receive broadcasts and specifies the correct intent to trigger. The last thing the method will do is set the correct RemoteControlFlags that we can process. This method will be called when we start or resume playback.

Whenever we stop music playback we want to ensure that we unregister the RemoteControlClient as we no longer need to display the lock screen controls.

Lastly, we can update the lock screen with the metadata we want to display. Calling the RemoteControlClient’s EditMetadata method will return a new MetadataEditor that allows us to set a plethora of information to be displayed with MetadataKeys. In this example I am hard coding in the song I am playing back with album artwork located in my drawables, but you might dynamically pass this information down to your service and download the artwork to display.

With these methods in place we just have to update our Start, Pause, and Stop logic to call these methods. Additionally, the RemoteControlClient has the SetPlaybackState method that we will set to Playing, Paused, Stopped, or Buffering so the lock screen will display the correct icons for each state. Now when we set the audio to playback we will register the RemoteControlClient, update the state, and set the metadata:

After the song is prepared we will again update the state and ensure the correct metadata is showing:

There you have it, a simple way to add great functionality to your audio streaming app. Try this project in Visual Studio, or run this Starter-compatible project in Xamarin Studio on Mac or Windows. I have the full source code available on GitHub, along with my original example of setting up the base streaming service.

Discuss this blog post in the Xamarin Forums