Airplay of audio using AVPlayer does not work in the background - ios

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?

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.

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

Related

Does background audio work without .longFormAudio policy on WatchOS?

Does anyone know wether audio can be both recorded and played back in the background on the Apple Watch?
It's possible to playback audio in the background by adding the .longFormAudio policy on the audio session however this does not work with the .playAndRecord mode.
I'd like to be able to record audio from the device's microphone then play it back either through the device speaker or paired Bluetooth headphones.
The audio will also need to continue recording and playing if the user lowers their wrist and the watch display turns off.

How to continue playing a youtube video at background when switch from my app back to home screen

Seems the default action for iOS 6 is to stop the video from playing after I switch to HOME screen. Then if I want to listen to the audio I have to go back to the app and press the 'Play' button again. I am using youtube API.
How can I continue playing the video when the application is in UIApplicationStateInactive and UIApplicationStateBackground. I just want to be able to listen to the audio even if I am on HOME screen or the screen is locked. Should I use a background thread or restart playing the video at the previous position as it was before turning the app inactive?
Take a look at the app 'Jasmine'. They managed to do something similar to what you are seeking. However, UIApplicationStateInactive/UIApplicationStateBackground will cause the video to pause. Double tapping the home button and then pressing the audio play button will cause the video's audio to resume without keeping the app active.
Take a look at the new iOS 7. You may have some luck there.
Inter-App Audio
Now your apps can make beautiful music together. With Inter-App Audio,
apps can register their audio streams to share with other apps. For
example, a series of apps could publish audio streams of instrument
tracks while another uses the combination of these streams to compose
a song. Inter-App Audio also provides for MIDI control of audio
rendering, remotely launching other registered Inter-App Audio apps
and more.

iOS 6 AVAudioSession issue

In my application I have made [audioSession setCategory:AVAudioSessionCategoryPlayback] to play audio in back ground and also to mute other audio play backs while playing song in my application. I am playing the audio using AVPlayer and also I have UIWebView for playing video in same Application. The issue arises when I play both video in web view and audio using AVPlayer. The music mixes in this case. Prior to iOS 6 when I rech same situation one audio pauses automatically and the mixing up does not occur. So kindly suggest some solutions.
Try to set
Required background modes -> App plays audio
in Info.plist.

How to start playing a sound while iPod's screen is turned off?

I need to play a custom sound synthesized using AudioUnit on an iPod.
This sound shall be played after up to one hour of playing music from a playlist on the iPod (it is played using MPMusicPlayerController).
The issue is that everything works fine if the screen in on, however my custom sound doesn't play if the screen is off.
If the sound is already playing when the screen is turned off it keeps playing well. So I assume I am using the correct audio session category.
Could you give me any hint?
According to Apple's developer library:
AVAudioSessionCategoryPlayback or the equivalent
kAudioSessionCategory_MediaPlayback—Use this category for an
application whose audio playback is of primary importance. Your audio
plays even with the screen locked and with the Ring/Silent switch set
to silent.
(Also, check this table.)
It seems that setting the audio session category to AVAudioSessionCategoryPlayback or kAudioSessionCategory_MediaPlayback would automatically fix your problem.

AVPlayer vs MPMoviePlayer

In my application I have to play videos of type .m3u8. These are not local video. I can do it either with MPMoviePlayerController or AVPlayer. I also have to ignore userinteraction when some advertisement is getting played. Also I have to give an option for Airplay.
Which player should I use?
Thanks.
AVPlayer has a subclass called AVQueuePlayer which allows you to play back several movies in a sequence. It would be helpful for you for your ads. AVplayer currently does not support Airplay.
From iOS 5 AVplayer has an option to start playing back automatically on Airplay by enabling usesAirPlayVideoWhileAirPlayScreenIsActive property (if allowsAirPlayVideo is also enabled)
As far as I know you cannot play on airplay automatically with MPMoviePlayer, user has to tap on the Airplay icon.

Resources