How to keep AVPlayer video playing when app goes background - iphone

I'm trying to keep the video in AVPlayer playing when app goes background, but it always stops. I use Swift. I've found a lot of examples, but nothing works.
I've turned on "App plays audio or streams audio/video using AirPlay" in Info.plist and added this code to the didFinishLaunchingWithOptions in AppDelegate:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch {
/*Error*/
}
What is the proper way to keep video playing?

Related

Playing Audio in Background at a certain time

Using Swift5.2, Xcode11.4 and iOS13.4,
I try to run Audio at a certain time in the future.
Moreover, the App is in background mode (and the App is completely closed).
The Audiosession must be set up to keep the App responsive and the Audio sound shall start at delay-times up to 1 year.
I tried:
A) The Background Mode "Audio, AirPlay and Picture in Picture" is added:
B) The AudioSession and AVAudioPlayer is configured as follows:
import AVKit
var audioPlayer: AVAudioPlayer?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?)
-> Bool {
do {
//set up audio session
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(.playback, mode: .default, options: [.defaultToSpeaker, .duckOthers])
try audioSession.setActive(true)
// Start AVAudioPlayer
try audioPlayer = AVAudioPlayer(contentsOf: alertSound)
audioPlayer!.numberOfLoops = -1 // play endlessly
audioPlayer!.prepareToPlay()
let currentAudioTime = audioPlayer!.deviceCurrentTime
let delayTime: TimeInterval = 20.0 // here as an example, we use 20 seconds delay
audioPlayer!.play(atTime: currentAudioTime + delayTime) // delayTime is the time after which the audio will start
}
catch {
print(error)
}
return true
}
.
There are two problems with the code above:
the session-Category .playback does cause the error Error Domain=NSOSStatusErrorDomain Code=-50 "(null)"
the sound only plays when App is in foreground or in background-with-app-still-alive - but unfortunately not in background-with-app-completely-closed
Here my questions:
Why is session-category .playback not working under iOS13.4 ?
Why is the sound not playing when the app is completely closed ? (background-mode) ???
Is it enough to just set the Background-mode tag "Audio, AirPlay and Picture in Picture" ? Or is there some code needed in order to fully implement background mode ? Is there something missing in code in order to make the sound future-start in a fully closed app ?
As for Question 1:
--> I've found a workaround and set the session-Category to .playAndRecored - after that the error goes away
try audioSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .duckOthers])
The question still remains: why is .playback not working ? And what is the difference between .playback and .playAndRecord ?
Why is session-category .playback not working under iOS13.4 ?
The .defaultToSpeaker option causes that error when used with .playback. I don't know why. If you remove it, it should work.
Why is the sound not playing when the app is completely closed ? (background-mode) ???
The audio session is killed when the app is killed. If you want to play sounds beyond the lifetime of your app, you need to use remote notifications. See "Push notifications method" in this article on building an alarm clock app:
http://andrewmarinov.com/building-an-alarm-app-on-ios/
Note that there are limitations with this method, mainly that it requires the user to have an internet connection at the alarm time to work. This is the current way to setup an alarm beyond the lifetime of the app, other than sending a simple local notification which has its own limitations on sound playback and is silenced by the hardware silent switch.
There are other methods in that link to keep your app open, but it will notify the user (keeping the mic always on, or subscribing to frequent location updates).
Is it enough to just set the Background-mode tag "Audio, AirPlay and Picture in Picture" ? Or is there some code needed in order to fully implement background mode ? Is there something missing in code in order to make the sound future-start in a fully closed app ?
This background mode keeps your app from being suspended while it has an active audio session. It does not prevent your app from being killed. Once killed, your app can't play any sound until it is opened again.

Cannot play after pausing tvOS (TVML/TVJS)

I am developing a tvml/tvjs application and I have added the following code to allow audio to play in the background (applicationDidBecomeActive):
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.setActive(true)
} catch {
print("no audio to assign")
}
I also set the first item in Required Background Modes in info.plist to App plays audio or streams audio/video using AirPlay.
My problem now is that although I can play audio in the background and pause the audio from anywhere in tvOS, I cannot play the audio after pausing. I am looking for the functionality like the Podcast app where I can freely play and pause the current streaming audio.
I cannot seem to find anything in the documentation for TVML/TVJS that points me in the right direction. Thanks!

Not getting sounds for Notifications or Messages in my Audio app when music is playing | AVAudioPlayer

I have an Audio app using AVAudioPlayer on iOS. Whenever music is playing my app and a new notification or a message comes, my player continues playing and there is no notification sound for the message.
I want that it should work in similar way default music app in iOS works, whenever a new message or notification comes music sound ducks for a while and notification sound is played and after that music starts playing normally again.
I read that setting kAudioSessionProperty_OverrideCategoryMixWithOthers to true should fix it as it allows mixing of sounds. I tried that as well, but it doesn't work and this also allows my app to play music along with other apps which I don't want.
All I want is whenever a message or notification comes when my app is playing music, I should get a notification sound and my app then continues playing.
Any help would be appreciated!
Finally after a lot of try I figured it out, I had set AVAudioSessionCategoryPlayAndRecord property which was causing the above error I mentioned. removing that fixed the issue

iPhone - Streaming audio with MPMoviePlayerViewController

I'm using MPMoviePlayerViewController to stream an audio from the URL "http://ios-audio.q-music.be/audio.m3u8".
Its playing well.
But when I click "Home" button, the app goes into background and stops streaming.
When I start the app again, it starts streaming again.
How can I make my app stream the audio using MPMoviePlayerController when the app goes to background.
Or do I've to use other frameworks?
Well I use https://github.com/mattgallagher/AudioStreamer for streaming audio, It will work in background.
http://digitaldj.net/2010/08/25/ios4-background-audio-revisited/ points to a fork of Matt Gallagher's original AudioStreamer code at http://github.com/DigitalDJ/AudioStreamer, which I can verify does support background audio. That said the original AudioStreamer has been updated this year so I'd imagine it also has implemented background support by now.
Certainly using MPMoviePlayerController to do background audio would be interesting since it presumably opens up the opportunity to support AirPlay directly in an audio streamer application.

iPhone Sdk Listening for Background Audio

I have an app that plays background music using AVAudioPlayer. The app also has youtube videos in it. When the user taps on the youtube video, I need the background audio to turn off. When they have either completed watching the video, I need the audio to resume.
The youtube video is getting played by embedding the video into an UIWebView. View Eample Here
I have tried using the delegate methods for AVAudioPlayer -
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
However, audioPlayerEndInterruption never winds up getting called.
So ultimately, I need a way to stop the music when the video is launched and resumed once the user is done with the video.
Method that could work but am not sure how to implement:
-Have a timer that checks for any audio playing on the device, check to see if it is not the audio player(would be the youtube player), then stop the background audio
If anyone has any suggestions, I would really appreciate them