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.
Related
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.
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)
Is there a way to get the MPMusicPlayerControllerNowPlayingItemDidChange notification while the app is in the background?
I have an app that needs to be able to pause the music once a song has ended while in the background or when the screen is locked. I'm using a systemMusicPlayer to play the music.
I've tried adding the audio background capability and including a call to beginBackgroundTask in my applicationDidEnterBackground but that doesn't work for extended periods of time.
When in background, your app might be killed at any time, so you do not want to depend on acting in background.
How I understood, what you want to achieve is that when your app goes to background, the currently playing music goes on but stops after the currently playing song finished.
MPMusicPlayerController.systemMusicPlayer() is playing a queue of songs ("Playlist"). So I would try to manipulate this queue in applicationWillResignActive() to not have a song after the currently playing one .
I did not test this and I am not sure whether or not this is possible through public API.
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