Ios 7 is not an outdated solution to resume background music from ducks

I can avoid background sound while playing new sounds. However, I cannot resume the background sound level to the maximum again. When my delegate tries to "decorate," he just holds on. The usual solution for this is AudiosessionSetProperty, but deprecated in iOS 7 and Apple does not give any hints in warnings or documentation on obsolescence.

I call this method when loading a view.

- (void) configureAVAudioSession { //get your app audioSession singleton object AVAudioSession* session = [AVAudioSession sharedInstance]; //error handling BOOL success; NSError* error; success=[session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error]; if (!success) { NSLog(@"AVAudioSession error :%@",error); } else { } success = [session setActive:YES error:&error]; if (!success) { NSLog(@"Error setting active %@",error); } else { NSLog(@"succes settings active"); } } 

This is when I play audio

 -(void)playTimeOnGo { NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"just-like-magic" ofType:@"mp3"]]; self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; self.audioPlayer.delegate=(id<AVAudioPlayerDelegate>)self; //get your app audioSession singleton object AVAudioSession* session = [AVAudioSession sharedInstance]; //error handling BOOL success; NSError* error; success=[session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error]; [self.audioPlayer prepareToPlay]; [self.audioPlayer play]; AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); } 

This is my delegate when sound is made to resume background sound and disconnect audio

 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag{ [self configureAVAudioSession]; NSLog(@"playback ended"); } 

So, how do I turn off background music again without outdated APIs? call [self configureAVAudioSession]; doesn't seem to work.

+7
deprecated background ios7 audiosession
source share
1 answer

Dear Sirs, I provide you with an inexhaustible working example of how to use ducking properly in iOS 7.

In your "view loading method" call this method, where bool is set to "YES". this will mix the background sound and prepare it for diving

  - (void) configureAVAudioSession:(bool) active { //get your app audioSession singleton object AVAudioSession* session = [AVAudioSession sharedInstance]; //error handling BOOL success; NSError* error; success=[session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error]; if (!success) { NSLog(@"AVAudioSession error :%@",error); } else { } success = [session setActive:active error:&error]; if (!success) { NSLog(@"Error setting active %@",error); } else { //NSLog(@"success settings active"); } } 

Play the audio file using this method. See how ducking happens. Remember to set the delegate as I am in this example EVERY time you play a new sound alert. do not install it once when viewing boot methods.

 -(void)playTimeOnGo { NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] //alertName is the name of your audio file without extention. extenstions is, doh extenstion like "mp" pathForResource:_dataManager.optionsSettings.setStarts.alertName ofType:_dataManager.optionsSettings.setStarts.alertExtension]]; self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; self.audioPlayer.delegate=(id<AVAudioPlayerDelegate>)self; //get your app audioSession singleton object AVAudioSession* session = [AVAudioSession sharedInstance]; //error handling BOOL success; NSError* error; success=[session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error]; [self.audioPlayer prepareToPlay]; [self.audioPlayer play]; AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); } 

the delegate will call this method after playback and increase the volume of background music :) Please do not confuse that I use the stream for this. You can simply call the method if you want, but this stops the main thread for about a second, maybe even two seconds. So this is normal with you, do not use a thread and just call [self configureAVAudioSession:NO];

 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag { _playBackFinishedDelegateThead = [[NSThread alloc] initWithTarget:self selector:@selector(configureAVAudioSession:) object:NO]; [_playBackFinishedDelegateThead start]; } 

This example is 100% tested and works in my application.

+4
source share

All Articles