MPMoviePlayer stops playing going in background - ios

need to play audio in background with MpMoviePlayerController. I know that I have to use this code to achieve that:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
and set the "RequireBakgroundMode" in the pList.
This is working fine except for that when I put my app in background the sound stops and I have to open control center and press play to resume playback.
So my question is, how can I avoid the sound stopping when the app goes in background? Thanks

Set of Capabilities of you project.its working fine when apps enter in Background.
When you make enable mode then in plist file set background mode automatically.

Related

AVAudioSession setCategory not working

I have a video capturing app and I want to be able to play background music while recording audio+video.
I can accomplish this if I set the AVAudioSession category to PlayAndRecord in didFinishLaunchingWithOptions. However, this causes a glitch in the audio whenever the view with the camera enters or exits the foreground, and its apparently impossible to get rid of: https://forums.developer.apple.com/message/74778#74778
I can live with the glitch if it just happens when I start/stop recording video, but that means I need to change the AVAudioSession category from Ambient to PlayAndRecord when a button is pressed, and this also seems to not be possible.
How do I change the AVAudioSession category?
I tried this:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];
But the background music stops when the recording starts.
Following this post: AVCaptureSession and background audio iOS 7
I tried deactivating the session then setting the category:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
[session setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker
error:nil];
[session setActive:YES error:&error];
I also tried listening for the AVAudioSessionRouteChangeNotification and setting the category in that handler after deactivating the session. But the background music always turns off when the app starts recording.
Why can't I change the AVAudioSession category?
Please try this category
AVAudioSessionCategoryMultiRoute
or check both categories
AVAudioSessionCategoryPlayAndRecord | AVAudioSessionCategoryRecord

iOS AVAudioSession ducking is slow and synchronous

In iOS, I'm trying to duck the Music app's music when playing some sound effects. In case you don't know, "ducking" simply means that the music volume gets a bit down before playing my sound, then the sound plays, and then the music volume gets back its initial volume.
For ducking, I'm setting AVAudioSession category to AVAudioSessionCategoryAmbient with option AVAudioSessionCategoryOptionDuckOthers, and then activating/deactivating the session (and playing the sound in-between, obviously). It works well, but the volume changes seem to be done in the same thread as the call, and the app hangs while the volume is being modified.
If you want to replicate the behavior, I think the fastest route is to start a new SpriteKit project, which will give you the sample, ship rotating project. Then put the following code in the touchesBegan:withEvent method:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions: AVAudioSessionCategoryOptionDuckOthers error: nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[AVAudioSession sharedInstance] setActive:NO error:nil];
Next run the app in an iOS device, put some music in the Music app and touch the screen to create ships and duck the music. You'll hear the ducking, but also see the ships freezing on the screen.
Is this normal? What would be the simplest way to avoid the app freezing while the ducking is made?
By the way, I'm using an iPhone 5S on iOS 8.1. Also, I'm using this in a Unity3D plugin. How can I duck the Music app from Unity itself?
You can try putting the AVAudioSession calls on a different thread. Then they won't be blocking the main (UI) thread. This is especially for setActive, which takes a noticeable amount of time to complete.
dispatch_queue_t myQueue = dispatch_queue_create("com.myname.myapp", nil);
dispatch_async(myQueue, ^{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions: AVAudioSessionCategoryOptionDuckOthers error: nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
});
This question also seems relevant: iOS AudioSessionSetActive() blocking main thread?

iOS: Keep playing music in background with Spotify SDK

I'm using the Spotify iOS SDK and have a question about the background music.
When a user locks the phone or presses the Home button I want my app to continue to play the music. How is this possible?
I've tested
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
Together with adding "Required background modes" to the apps plist.
Does this require that the AVAudioPlayer is used? Since I'm using the Spotify streamingPlayer its not possible.
Background playback should work just fine. You do need the audio item in your UIBackgroundModes Info.plist entry, though. Also, make sure you test on a device - I know remote control events don't work in the iOS Simulator, and that might be the case for background audio as well.
The iOS SDK will automatically set up the AVAudioSession for you, so you don't need to do that.
You might find this answer helpful, which is more in-depth: Background Audio with cocoalibspotify.

Stop UILocalNotification Sound on slide to view

In my alarm app, I am scheduling an alarm on some time.
With the app in foreground I lock my iPhone (iOS7, I haven't tried with iOS6).
Now when the notification sound starts, I view the local notification by using the "Slide to view" on my lock screen, and when the app opens, I also play the sound using AVAudioPlayer.
So the problem is notification sound continuously ringing until 30.0 seconds.
Is this iOS issue or in the code I need to integrate any method etc.?
Thanks
according to another SO post, this is a bug with iOS 7 where the sound doesn't stop when the user 'slides to view' if passcode lock is turned off.
possible fixes can include turning on passcode lock or using this code (taken from the linked post).
- (void)applicationWillEnterForeground:(UIApplication *)application
{
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[musicPlayer setVolume:0.0f];
}
This bug has been fixed as of iOS 8

iOS MPMoviePlayerViewController stop playing when screen locks

I implement MPMoviePlayerViewController to play video and I enable airPlay and works great but the problem is when the screen on the ipad locks. My question is how can make sure the MPMoviePlayerViewController still in airPlay even when the screen locks. Any of you knows how can I make this work?
Where as Apple thinks that When user can't see the video what is the benefit to runs consistently? So there is no need to play Video when you can't see it. Apple pauses it when app goes to background or screen locks. If you still want to play it you can add notification to start and stop when you go to background and come to foreground.
Hope this helps.
In your Info.plist file, add the key "Required background modes" with the value "App plays audio or streams audio/video using AirPlay".
Also, when you receive the notification MPMoviePlayerIsAirPlayVideoActiveDidChangeNotification, add these lines:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
And add this line to your dealloc method:
[[AVAudioSession sharedInstance] setActive:NO error:nil];

Resources