Reputation: 1743
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 :-(
Upvotes: 2
Views: 3065
Reputation: 33
I know this is an old question but just to add what I experienced:
AVAudioSession.SharedInstance (); did not work on my device but creating a new instance does AVAudioSession audiosession = new AVAudioSession ();
In info.plist RequiredBackgroundModes value is now "Audible content" as opposed to "Audio"
Hope that helps.
Upvotes: 0
Reputation: 33
I had a similar problem where one view was recording and another view played back that recording. Recording would only work sometimes (would produce 5kb files) that were empty. No error message displayed. After hours of frustration I came across the above post by Dermot and got it to work by adding AVAudioSession
before the AVAudioRecorder
:
AVAudioSession audioSession = AVAudioSession.SharedInstance ();
audioSession.SetCategory (AVAudioSession.CategoryRecord,out error);
audioSession.SetActive(true, out error);
recorder = AVAudioRecorder.ToUrl (url, settings, out error);
And the same code but changed CategoryRecord
to CategoryPlayback
.
audioSession.SetCategory (AVAudioSession.CategoryPlayback,out error);
Upvotes: 0
Reputation: 1743
Ok, I feel a little stupid. It turns out my key UIApplicationModes had a space in front of it. Doh!
Well, to anyone having similar problems, ensure your info.plist looks like this
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
and setup your playback code like this (remember, this is Monotouch):
AVAudioSession audioSession = AVAudioSession.SharedInstance();
NSError error;
audioSession.SetCategory(AVAudioSession.CategoryPlayback.ToString(),out error);
audioSession.SetActive(true,out error);
UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
AudioPlayer = new AVPlayer();
Upvotes: 2
Reputation: 8163
Seems you didn't set up the AVAudioSession
properly. Take a look at this and this.
I hope those tips guide you in the right direction.
Upvotes: 1