Firstly, I understand this is a copy of a similar objective C based question, but it wasn't answered.
Im using AVPlayer for the audio in my app. I have it playing audio, setting up its audio session correctly (I think, the play icon appears in the status bar when I play audio tracks within my app, and my app icon appears beside the player transport controls in the multitasking dock). However, I cannot get it to continue playing in the background when I press the Home button. The music just fades out and stops.
I have the following key in my info.plist:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
In my class that handles playback, I have:
AVAudioSession audioSession = AVAudioSession.SharedInstance();
NSError error;
audioSession.SetCategory(AVAudioSession.CategoryPlayback.ToString(),out error);
audioSession.SetActive(true,out error);
UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
int taskID = 0;
taskID = UIApplication.SharedApplication.BeginBackgroundTask(delegate
{
if(taskID !=0)
{
UIApplication.SharedApplication.EndBackgroundTask(taskID);
taskID = 0;
}
});
PlaylistManager = new ApolloPlaylistManager();
AudioPlayer = new AVPlayer();
Not sure if I need that BeginBackgroundTask part, but it doesn't work if its in or out, im actually pretty sure I dont need it. Any tips at all greatly appreciated, as I've been stuck on this for a few days :-(