AVAudioSession endInterruption () returns NSOSStatusErrorDomain

I am trying to solve the AVAudioSession problem since many hours ago and did not get success!

I found many guys who talked about problems with endInterruption, but none of them talked about this error:

Unable to reactivate the audio session after the interruption ended: Error Domain=NSOSStatusErrorDomain Code=560161140 "The operation couldn't be completed. (OSStatus error 560161140.)" 

I tried converting this code to ASCII to check the results of the audio code, but nothing is related to this number.

My code to start the session:

 - (void) setupAudioSession { mySession = [AVAudioSession sharedInstance]; [mySession setDelegate: self]; NSError *audioSessionError = nil; [mySession setCategory: AVAudioSessionCategoryPlayback error: &audioSessionError]; if (audioSessionError != nil) { NSLog (@"Error setting audio session category: %@", [audioSessionError description]); return; } // Activate the audio session [mySession setActive: YES error: &audioSessionError]; if (audioSessionError != nil) { NSLog (@"Error activating audio session during initial setup: %@", [audioSessionError description]); return; } // Prepare audio file to play NSBundle *mainBundle = [NSBundle mainBundle]; NSURL *bgSoundURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"bg_sound" ofType:@"aif"]]; bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgSoundURL error:&audioSessionError]; if (!bgPlayer) { NSLog(@"No bgPlayer: %@", [audioSessionError description]); } [bgPlayer setNumberOfLoops:-1]; [bgPlayer prepareToPlay]; 

}

And the code of the endInterruption method:

 - (void) endInterruptionWithFlags: (NSUInteger) flags { if (flags & AVAudioSessionInterruptionFlags_ShouldResume) { NSError *endInterruptionError = nil; [[AVAudioSession sharedInstance] setActive: YES error: &endInterruptionError]; if (endInterruptionError != nil) { NSLog (@"Unable to reactivate the audio session after the interruption ended: %@", [endInterruptionError description]); return; } else { NSLog (@"Audio session reactivated after interruption."); if (interruptedWhilePlaying) { self.interruptedWhilePlaying = NO; // Resume playback by sending a notification to the controller object, which // in turn invokes the playOrStop: toggle method. NSString *MixerHostAudioObjectPlaybackStateDidChangeNotification = @"MixerHostAudioObjectPlaybackStateDidChangeNotification"; [[NSNotificationCenter defaultCenter] postNotificationName: MixerHostAudioObjectPlaybackStateDidChangeNotification object: self]; } } } 

}

Most of this code was extracted from the Apple Mixer host example. And it is strange that the sample works great!

Can you find out what is wrong?

Thanks.

+2
source share
4 answers

I solved a problem changing the way I design my audio code. Instead of using AVAudioSession and its delegation methods, I move on to C style for working with audio.

I implemented a function:

 void interruptionListenerCallback (void *inUserData, UInt32 interruptionState) {} 

And it was initialized with:

 AudioSessionInitialize (NULL, NULL, interruptionListenerCallback, self); 

inside the my - (id) init method.

Hope this helps you too. Best.

+3
source

"If this delegate method gets the constant AVAudioSessionInterruptionFlags_ShouldResume in its flags parameter, the sound session is immediately ready to use."

You did not properly handle the callback. When you receive AVAudioSessionInterruptionFlags_ShouldResume, your audio session is ALREADY ready to use. You should call setActive when you get the DIFFERENT flag.

Hope this helps ...

+2
source

Using @Trinca, answer what I did in the ARC'ed project:

 AudioSessionInitialize (NULL, NULL, interruptionListenerCallback, (__bridge void *)(self.player)); 

If self.player is an instance of your player, you go to the callback function.

Then call back:

 void interruptionListenerCallback (void *inUserData, UInt32 interruptionState) { NSLog(@"itnerp state %li",interruptionState); NSLog(@"inUserData %@",inUserData); AVAudioPlayer *pl = (__bridge AVAudioPlayer*)inUserData; switch (interruptionState) { case 0: [pl play]; break; case 1: [pl pause]; break; } } 

LUCK

Shani

+1
source

use audiosession.setmode if it's voip

0
source

Source: https://habr.com/ru/post/1265300/


All Articles