In this recipe, we will learn how to prevent the app from being suspended in order to allow audio playback.
Create a new Single View Application in Xamarin Studio and name it BackgroundAudioApp
. Add a button on the view of the controller. You will also need an audio file. In this example, an M4A file with a duration of 21 seconds is used.
Perform the following steps:
Double-click on the
Info.plist
file to open it. Select the Source tab at the bottom and add theUIBackgroundModes
key (Required background modes) with the string value audio. The following screenshot shows you how the key and value are shown in the editor after they have been set:Add the
MonoTouch.AVFoundation
namespace in theBackgroundAudioAppViewController.cs
file.Enter the following
ViewDidLoad
method in the class:private AVAudioPlayer audioPlayer; public override void ViewDidLoad () { base.ViewDidLoad (); NSError error = null; AVAudioSession.SharedInstance(...