O'Reilly logo

Xamarin Mobile Development for Android Cookbook by Matthew Leibowitz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Playing audio in the background

Sometimes, we may want to be able to play audio in the background after the user has closed the app. We may be creating a media player app, or we may just want to allow various media to be played without requiring an activity to be displayed on the screen.

How to do it...

We can play audio in the background if we play it from a Service instance:

  1. First, we will create a service that will be responsible for playing music. Here, we have methods that will be expanded in the following code snippet:
    [Service] public class MediaService : Service { public const string ActionPlay = "ActionPlay"; public const string ActionPause = "ActionPause"; public const string ActionStop = "ActionStop"; private MediaPlayer mediaPlayer; private ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required