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 ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.