AVPlayer pausing when app become inactive. How to automatically restart when active - ios

An AvPlayer will pause when the app goes in the background, and stays paused when the app goes back to the foreground. Is there an easy way to make the video play again (if it was playing when the background event happened) without having to do it manually for each AVPlayer ?
I see how I can have notification observers that will play my AVPlayers and that I will have to remove when the player gets deinit, but that's annoying.

Related

How to loop a queue of songs in AVQueuePlayer even when device is locked?

I already have a looping implementation, and it works when the device is open. I listen for the AVPlayerItem.DidPlayToEndTimeNotification and handle the looping in that Callback. But as soon as the device is locked, it the AVQueuePlayer does not play. Is this somehow related to iOS backgrounding and how apps function when backgrounded?
Normally, Apple does not allow apps to run in background. If you want to play a song in background, you should set the Required Background Modes property in the application's Info.plist.
Audio - Music players and other applications that work with audio
content may be registered to continue playing audio even when the app
is no longer in the foreground. If an app in this category attempts to
do anything other than play audio or download while in the background,
iOS will terminate it.
Look at the document and ask for permission.
I fixed the problem. When an AVPlayer stops playing, the app is suspended by iOS even with Background Modes enabled. My looping logic is implemented right after the AVPlayer plays the last item so it doesn't get executed in the background.

iOS AVPlayer Entering the foreground will play automatically

I use AVPlayer to play video.I haven't added any NSNotifications yet.
When the app goes into the background, video will pause, which is what I expected. But when the app goes back to the foreground, video will play again. And I'm going to pause video, and then I'm going to go back in the background, and then I'm going to go back to the foreground, and video is going to play again. Why is that?
1. Why does video automatically play when it enters the foreground from the background? I didn't do anything...
2. What should I do? Getting video from the background into the foreground does not play automatically
use the below fun to make the video pause when going into background.
func applicationWillResignActive(_ application: UIApplication)

How to stop MPMusicPlayerController from playing when application terminate by user?

I have the following situation in my application:
I have a music system in my application and I used MPMusicPlayerController to play music; every thing is working fine until now.
My problem: When a user starts playing music in my application and after some time it terminates, music cannot be stopped because I'm using the [MPMusicPlayerController systemMusicPlayer] object. I know there is another option which is applicationMusicPlayer, but it stops playing music in background, which doesn't satisfy my requirements.
How can I stop the music from playing when the application is terminated by user?
I have some code that attempts to stop it in applicationWillTerminate: but it only works in some situations:
If I press home button twice and terminate the app from the multitasking UI, then the app can stop the music player.
If I press the home button once and then go to the home screen, and after that I press the home button twice and terminate the application then it can not stop my music player.
I tried to put a breakpoint in applicationWillTerminate: but in the second example (from above), the application crashed and did not execute my code, unlike in the first situation.
UPDAT
And I know that when I use MPMusicPlayerController background mode is not required because it starts music in the native music player.
Any help would be appreciated.
Unfortunately your only option based on your requirements is to play the audio using your own app, AVAudioPlayer for example and set the audio background mode requirement.
As there is no way of receiving a notification when your app is terminated by the OS or by the user, you have no way around it, you will not be able to stop the system or application player from your process.
Registering for NSUncaughtExceptionHandler wont catch SIGKILL or SIGSTOP either so you cannot rely on the system music player.
This shouldnt be a problem though as its easy to setup audio playback in your app. If you are using the system player so that the user can play their own music, you should use a picker and access their music library so that they can choose the music.
In this case:
If I press the home button once and then go to the home screen, and after that I press the home button twice and terminate the application then it can not stop my music player.
You have to register application in the background mode.
fileprivate var backgroundTask: UIBackgroundTaskIdentifier = .invalid
func registerBackgroundTask() {
self.backgroundTask = UIApplication.shared.beginBackgroundTask {
//TODO
}
}
Call it in AppDelegate
Hope to help you

Preventing my app's audio from interfering with other background music

I think what I'm looking for is the combination of existing settings that I can't quite put together in the right order. I have an app that plays sound clips using AVAudioPlayer. There is a start/stop button.
I would like any other apps' audio to continue until my app actually needs to play something.
When my app is launched with some iTunes music playing, that music continues correctly. When I press play in my app, the other audio correctly stops, and my app plays. I stop my player, switch back to the music app, and press play again. But now when I switch back to my own app, the music stops immediately.
I would like it to again wait until it needs to play something before killing the music app.
My best guess is that once I've used the player, it is in some kind of 'I need audio' state, and remains in that state. How do I resign this state?

iOS 4.3 AudioQueuePause vs home button

I use AudioQueue to play sound in my game. There are two sound objects: looped music and non-looped sfx. The problem that I observe is as follows:
The music is started.
The sfx is started.
The sfx is paused (using AudioQueuePause)
The game goes to background by Home Button.
I get UIApplicationWillResignActiveNotification and the music is paused (using AudioQueuePause).
I return to my app from iPad menu.
I get UIApplicationDidBecomeActiveNotification and the music is unpaused (using AudioQueueStart).
Note that I did not unpause sfx. In spite of that, it resumes and I can hear it!
I can only reproduce it on iPad with iOS 4.3 when going to background by Home Button. If I use Sleep/Wake button instead of Home, it works perfectly.
So it seems that the system somehow resumes all audio queues : even explicitly paused ones.
Any thoughts?

Resources