I'm trying to figure out how to keep AVAudioRecorder recording even if the device goes to sleep (or to keep the device from going to sleep to begin with).
According to this question I just need to add a category to the AVAudioSession, but that doesn't seem to be working.
I also found MMPDeepSleepPreventer but every time it plays a silent sound it interrupts the recording.
So is there a way to get AVAudioRecorder to continue recording after the device sleeps. OR is there a way to stop the device from sleeping in the first place?
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:NULL];
You can stop the device from sleeping by adding
[UIApplication sharedApplication].idleTimerDisabled = YES;
But I am not sure whether Apple encourage using this.
Related
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?
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.
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
We've almost finished porting our app to iOS7, but we've ran into an issue that AVPlayer defaults playback on the iPhone to the receiver (quiet speaker) instead of regular loud speaker. A solution for that appears to be using
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback ...];
this, however, on iOS7 pops up a dialog requesting microphone permission. Why? How can I avoid this, as the app doesn't record anything? We're using AVPlayer for playback and also have background audio permission.
Have you tried to override output rote of Audio session? I've had similar problem on iOS 6 when sound was played on loudspeaker on iPod but on phone speaker on iPhone.
UInt32 audioRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(UInt32), &audioRouteOverride);
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.