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)
Related
I play audio from a webpage using SFSafariViewController and when the app goes to background, the audio stops.
It happens when user blocks the screen as well.
Any ideas how to keep audio playing?
I am developing a project that play music in the background. Basically, the app will continue play the audio file even the screen is locked.
I have setup the background mode by selecting audio option, as you can see in this photo:
It works fine as I expected that the app keep playing the audio when users lock the screen.
However, when a call is received, the app stop playing. I thought it may start playing again after the phone call finished. However, it didn't.
I check a few webs, should I do some modification in the AppDelegate file, such as this method:
func applicationWillResignActive(_ application: UIApplication)
Basically, what I am trying to figure out is, how can I pause the audio when phone call or other notifications come and resume playing the audio when the phone call or other notifications finished.
Thanks in advance.
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.
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
I have used AVAudioRecorder. The recorder is not recording when i come from background.
My Scenario is
Start recording press home button (Recording stopped)
Open my app the recorder is still recording (Checked by recorder.isRecording is YES)
Stop the recording and playing the recorded audio plays only what recorded before my app goes background.
I am recoding 5sec -> app goes background -> comes fore foreground -> record another 5 secs -> play the audio the audio plays only the first 5 secs.
While recording I am checking the size of the file path given to the audio recorder(By NSTimer) the file size is not increased after come from background but still the recorder status is recording
Can any one point out what is the mistake. Thanks
Note: in ios6 there is no problem it plays entire audio that is entire 10 sec it comes only in ios7
So if you dont want to record in the background and you only want to append your recording before and after going to background, pause your recorder in applicationWillEnterBackground (or applicationWillResignAcive) and when you come back form background, continue your recording and you'd be fine. (You can have notifications letting your class which is recording know about those life cycle delegate methods.)
But if you want to record in the background you need to turn on Background Mode: Audio and Airplay in your app settings -> Capabilities. But thats another story.
You should check AVAudioSessionDelegate delegate method. Audio recording will stop during phone call
- (void)beginInterruption{
}
-(void)endInterruption{
}
You can continue background recording. Add the background modes as seen in image