Ducking iPod audio while in background
Ducking iPod audio while in background
- Subject: Ducking iPod audio while in background
- From: Brett Jones <email@hidden>
- Date: Thu, 21 Nov 2013 22:20:52 -0500
I’m making a fitness app that plays iPod music and voice prompts. I’ve been playing with how to do this in the background for a while now and haven’t come up with a successful solution yet. The idea is simple though - queue up a few iPod songs and, at certain times in the future, duck the iPod audio, play a recorded audio clip, then unduck. I have this working while the app is in the foreground using AVPlayer and MPMusicPlayerController (and have had it working using Audio Queues as well), but when I lock the phone, it fails. Here are some of the issues and questions:
1. If the app enters the background without an active audio session and audio currently playing, scheduled music never plays.
2. While the app is in background, timers don’t tick, so scheduling ducks with dispatch or NSTimer never fire either.
3. I’ve considered ripping audio from its MPMediaItem and saving to a local file using AVAssetReader. Does this work on all music or does it fail for some music, for example DRM protected songs?
4. If 3 works for any song, I think I could use an Audio Unit setup to fake ducking, but that’s only if most/all iPod music can be ripped in that fashion.
5. The following code is how I trigger ducking currently. It works, but is it the right way?
- (void)mixOn {
NSLog(@"mixing");
NSError *err;
[[AVAudioSession sharedInstance] setActive:NO error:&err];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&err];
[[AVAudioSession sharedInstance] setActive:YES error:&err];
if (err)
NSLog(@"%@",[err localizedDescription]);
}
- (void)duckOn {
NSLog(@"ducking");
NSError *err;
[[AVAudioSession sharedInstance] setActive:NO error:&err];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:&err];
[[AVAudioSession sharedInstance] setActive:YES error:&err];
if (err)
NSLog(@"%@",[err localizedDescription]);
}
- (void)duckOff {
NSLog(@"duck off");
NSError *err;
[[AVAudioSession sharedInstance] setActive:NO error:&err];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&err];
[[AVAudioSession sharedInstance] setActive:YES error:&err];
}
So the question again is - how can iPod music be ducked while the phone is locked/app is in background?
Thanks!
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden