Running MPMoviePlayer when the App goes into background - ios

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

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 Cordova Media Plugin in background: MEDIA_ERR_NONE_SUPPORTED = 4

When running my app in background, I would get MediaError.MEDIA_ERR_NONE_SUPPORTED (4) when trying to create and play an audio file.
While most suggestions for this error relates to file format, file existence or audio recording, my audio files were playing perfectly when the app was in foreground.
Note: I am already using the Cordova Background Mode plugin.
It turns out that the Cordova Music Controls is causing this interference.
Without manipulating the music controls, audio plays perfectly even when the app enters background.
The problem came from destroying the music controls, which makes iOS prevent your app from playing audio until the user resumes the app to foreground.
Turns out you don't have to destroy music controls before creating them again, to update the track label.
TLDR; Don't kill music controls while playing audio in the background.

Playing audio in a WatchKit workout app when backgrounded and screen is off

I'm trying to build a WatchKit app that will give audio feedback during a workout. The audio is a series of short pre-recorded voice clips.
The app starts a workout session
Audio clips play fine when the app is active
Audio also plays fine when the app is in the background whilst the screen is on (e.g. by tapping the digital crown, or opening another app).
However, when the screen turns off - e.g. by starting a workout and then lowering the wrist - the audio doesn't play until the app becomes active again.
i.e.
Start workout + keep app on screen = sound plays ok.
Start workout + move app to background by tapping digital crown + keep screen on = sound plays ok.
Start workout + lower wrist = screen switches off and sound no longer plays.
Start workout + move app to background + lower wrist = screen switches off and sound no longer plays.
I'm trying to figure out what I've missed in order to support audio in a workout app when the screen is off...
The app has an active HKWorkoutSession.
The extension has the "workout-processing" background mode enabled under WKBackgroundModes
The extension also has the "audio" background mode enabled under UIBackgroundModes.
The extension has the HealthKit entitlement and all HealthKit features are working.
On the iOS app, I've added the 'audio' background mode.
I've tried:
playing the audio clips using AVAudioPlayer
playing audio using AVAudioEngine+AVAudioPlayerNode
giving up on the clips entirely and using AVSpeechSynthesizer
The behaviour is the same - as soon as the screen switches off, the sound no longer plays. The sound does play if the app is in the background and the screen is still on.
The app is running on Watch OS 4. I've tested on a Series 2 and a Series 3 watch and it is the same on both.
What have I missed?
Ahhh, finally I found this post and realised I had the same problem:
The AVAudioSession category needs to be set to AVAudioSessionCategoryPlayback (I had it set to ambient).
As soon as I changed the session category to AVAudioSessionCategoryPlayback it worked!
To play audio while in background you also need to set the UIBackgroundModes to "audio" in the extension plist apart from setting the "workout processing" background mode.
Refrence HKWorkoutSession
To play audio or provide haptic feedback from the background, you must also add the UIBackgroundModes key to your WatchKit extension’s Info.plist file. This key’s value is an array containing the audio value.

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

Disable app's background mode when audio works not activate

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.

Resources