I am trying to stream a remote mp3 using AVPlayer.
I have set up the Audio Session, and added the Plays Audio In Background to my info.plist file.
I am running this code:
self.timeObserver = [self->player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:nil usingBlock:^(CMTime time) {
[self updateControls];
}];
to add a time observer, that calls updateControls every second.
Thing is, when the sound buffer is low, the player just pauses, and this code in the background is stopped, so I have to manually press play in the app again. (and if I pause the player, this code doesn’t run either)
Am I doing anything wrong? How can I run a piece of code the whole time in the background without interruptions?
You can’t run arbitrary code in the background indefinitely. You can keep the audio playing, but you shouldn’t be expecting to update your UI if you’re not the frontmost app. You can update the UI when your app moves back into the foreground.
As long as you set your AVAudioSession properly and make it active, and you have the UIBackgroundModes set, you should be good to go on the audio front.
see http://developer.apple.com/library/iOS/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Configuration/Configuration.html