Disable app's background mode when audio works not activate - ios

I'm using AudioUnit to handle the audio record and replay.
And I add audio to UIBackgroundModes key in info.plist to keep it recording or playing when enter background.
The problem is: when my app is not recording or playing audio, the app still running in background, and I can see the red alert bar when device locked or in home screen.
So, how can I enable my app to be suspended when it is not recording to save batt life? Just like before adding the UIBackgroundModes key?

Finally, I figured it out today.
I stop the audio file writing procedure, but do not stop audio session.
By add AudioSessionSetActive(NO) when stop playing, the problem solved.

Related

How to resume audio playback of other apps after deactiving the audio session and how to start audio playback in the background without mixing?

I am building a iOS app with Ionic and Capacitor. I use AVFoundation inside a Capacitor plugin which provides a public method to play an audio file accessible at a remote URL. The basics work, the audio file is successfully played. Now I want to correctly handle interruptions and background audio. By background audio I mean start playing an audio file while the app is in the background and not continue playing it in the background when it was started in foreground before. To summarize, the app should behave the following way:
If another app (e.g. Spotify) is playing audio and my app wants to play audio, the other app should be interrupted and my app's audio should start playing without mixing.
If my app's audio is finished playing, the other app's audio should resume.
My app must be able to start playing audio without mixing when it is in the background.
I want to show a play/pause button and the audio title on the lockscreen and in the notification center.
I added the background mode audio capability. I use AVPlayer, AVAudioSession, MPNowPlayingInfoCenter and MPRemoteCommandCenter, followed best practices, especially regarding activation and deactivation of an audio session and interruption handling. I followed this question and this question. I tried several solutions, but never managed to achieve all of the above goals. What I found out so far is the following:
Goal 1. above is easy to achieve. Simply activate an audio session without mixing and start playing the audio.
After the audio of my app is finished, I deactivate the audio session. Unfortunately, I can only achieve goal 2. above if I don't use remote commands nor notification info. This means, my app then has no audio control UI on the lockscreen nor in the notification center. Somehow the configurations of MPNowPlayingInfoCenter and MPRemoteCommandCenter seem to affect the interruption behavior. As far as I could see, Apple documentation doesn't say anything about this.
I only manage to start playing audio in the background when setting the option .mixWithOthers. Apple documentation doesn't say anything about this, either.
Any help would be greatly appreciated.
Rony

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.

How can I play music successively in the background with UIWebView or WKWebView

The website implemented inside my app would play a series of mp3 files,it works well when the app is active,but when I press HOME button or lock the phone,it cannont play more than ONE song in the background,after that,app suspended.
I've already tried to set different kind of AVAudioSession Category,but of no use.
I assume you don't have audio background mode enabled. This is what Apple documentation says:
When the UIBackgroundModes key contains the audio value, the system’s media frameworks automatically prevent the corresponding app from being suspended when it moves to the background. As long as it is playing audio or video content or recording audio content, the app continues to run in the background. However, if recording or playback stops, the system suspends the app.
So it seems like you just need to enable audio background mode. Here is Ray Wenderlich's tutorial on background modes

AVAudioRecorder doesn't append the recording after comes from background

I am using AVAudioRecorder for recording while I am recording I press home button and comes to fore ground and checked the status of the AVAudioRecorder (isRecording) is still YES. But when i play the audio it plays only what i recorded before goes to background.
Note: This problem comes only in ios7. It works well in ios6
Have you set the app to allow background recording? Under the Project Capabilities, turn on Background Modes and select Audio and AirPlay.
Be warned, in iOS7, when background recording through AVAudioRecorder is interrupted, the recorded file is closed instead of paused. If a user get a phone call while recording in the background, the audio file will stop recording and cannot be resumed. You will need to start a new audio file and merge them, or use Audio Queues and manage the interruptions yourself.
See this answer for more info: Audio interruption when iOS application is recording in background

Running MPMoviePlayer when the App goes into background

I am making an App where i m running am streaming a radio url. But when the App goes into background it stops running. I want to run it when when the App enter background.
Can anyone help me with this.
As suggested by P.J., your app should register for background audio play.
According to the Apple Doc
When the UIBackgroundModes key contains the audio value, the system’s
media frameworks automatically prevent the corresponding app from
being suspended when it moves to the background. As long as it is
playing audio or video content, the app continues to run in the
background. However, if the app stops playing the audio or video, the
system suspends it.
It's simple. You need to register as a background audio app by including the UIBackgroundModes key (with the value audio) in its Info.plist file.
Note: Apps that include this key must play audible content to the user while in the background.
Edit
For Xcode 4.6, go to plist-> Required Background Modes-> App Plays Audio

Resources