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:
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 bool isPrepared = false; public override void OnDestroy() { base.OnDestroy(); CleanUpPlayer(); } public override StartCommandResult OnStartCommand( Intent intent, StartCommandFlags flags, int startId) { switch ...