is it possible to play avaudioplayer in background mode? - ios

I am doing an Alarm application,in my app after lunching app in device i am saving one alarm and i close the app.When alarm is coming, its not ringing But at that time update method is calling i am getting notification. after that i set another alarm and i keep the app open at that time i got both alarm sound and one alertview after that all the alarms are also coming in background mode with sound.
I did change in info.plist in Required background modes and in coding also i am doing like this
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
Please suggest me what should i do?

For alarm purpouses it's better to use Local notifications or Calendar, I think.

Related

iOS AVAudioRecorder record without stopping background audio

I need to allow my app to record audio without stopping background music player.
I'm using AVAudioRecorder.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
However, anytime [audioplayer record] is called, the background music will stop playing.
What can I do so that background music doesn't stop playing?
Thanks.
You need to add the MixWithOthers Category Option as well:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];

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];

Background Audio in iOS

I'm trying to build an iOS App (iOS > 5.0). It should play music files when a timer, set by the user, ends. Here is my setup what if done so far:
I use avplayer
I've set Background Modes in my plist to "App plays Audio"
in my appDelegate I have:
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
before a song is going to be played a set:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
i notify my application on "playerItemDidPlayToEnd", stop the player and tell other apps like the music.app to become active again
_avPlayer = nil;
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation error:&sessionError];
This works fine. You can have your music app playing in background. My App starts to play a song the music app fades away. My song is played and if its finished the music app continues to play. But only as long as my app isn't in background.
In background I get the error:
Error Domain=NSOSStatusErrorDomain Code=560161140 "The operation couldn\u2019t be completed. (OSStatus error 560161140.)"
Is it possible that both combinations of, playing in background (on custom events) and switching between f.e. music.app and my app in background isn't possible?
Here a the code of my player model: http://pastie.org/4830995
I think your problem might be with background handling. You need to override this function on app delegate.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
__block UIBackgroundTaskIdentifier task = 0;
task=[application beginBackgroundTaskWithExpirationHandler:^{
NSLog(#"Expiration handler called %f",[application backgroundTimeRemaining]);
[application endBackgroundTask:task];
task=UIBackgroundTaskInvalid;
}];
}
your application will get killed after 6 minutes unless sound is played. If your user set timer for more then 6 minutes, what you could do is to play short quite sound every 5.5 minutes to keep you app alive and start new background task. Also you have to consider that lot of thing will not get executed in background. I did some stuff somewhat similar to what you are doing. What I did was created custom subclass of NSObject witch handled music playback and application expiration stuff. And made it a strong property on my delegate as lot of functions and notifications will not run on you VC in the background.
Although if you just want to play a sound this might not be the best approach. Maybe just sending local notification would do.
Hope this helped.
Did you register your app to keep running in the background as an audio app? To do so, add the UIBackgroundModes key to your Info.plist file and set its value to audio.

Resources