Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Airplay of audio using AVPlayer does not work in the background

I am doing audio playback using the AVPlayer class from a remote URL.

It plays fine if I just play to the headphones/built in speaker, but I am seeing some oddities with AirPlay.

When I change to use an AppleTV for AirPlay, it will stream the audio, but on my iphone the audio playback indicator (the small 'play' triangle in the top menu bar) disappears, and when I lock the phone instead of continuing in the background, it stops. (When playing normally, it plays in the background fine).

Why is the AVPlayer not working in the background for audio over AirPlay?

Is this what other see? Is there a way around it?

like image 510
Andrew Kuklewicz Avatar asked Jan 10 '12 17:01

Andrew Kuklewicz


2 Answers

Ok - I dug through the apple dev forums and found a hint.

If you are using AVPlayer for audio only, and want it to work in the background while doing airplay, you need to disable the allowsAirPlayVideo setting.

Apparently AVPlayer on iOS 5+ assumes that it is playing back video via AirPlay, and so does not allow backgrounding, unless you explicitly disable video AirPlay.

Once you disable this (i.e. self.player.allowsAirPlayVideo = FALSE;) then your audio will still play via AirPlay, but now it will not be treated as video, only as audio, and so will allow the backgrounding to work.

like image 57
Andrew Kuklewicz Avatar answered Nov 06 '22 09:11

Andrew Kuklewicz


Have you enable the 'Background Audio' mutlitasking setting in the app plist?

enter image description here

like image 43
ACBurk Avatar answered Nov 06 '22 09:11

ACBurk