iOS8 AVAudioSession setActive error

xialin picture xialin · Jun 9, 2014 · Viewed 19.5k times · Source

I'm testing my app in XCode 6 and find an issue with AVAudioSession in iOS8.

When I call

[[AVAudioSession sharedInstance] setActive:NO error:nil];

I get the following error message:

AVAudioSession.mm:623: -[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.

In AVAudioSession.h, it says

Note that this method will throw an exception in apps linked on or after iOS 8 if the session is set inactive while it has running or paused I/O (e.g. audio queues, players, recorders, converters, remote I/Os, etc.).

But I'm not sure how can I check if there's running I/O and how can I dispose all when I need to reset the audio session.

Answer

Alessio Campanelli picture Alessio Campanelli · Oct 9, 2014

I solved this problem, inserting this code in the method of AVAudioPlayerDelegate.

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
  [audioPlayer stop];
  [audioPlayer prepareToPlay];
}

where audioPlayer is a variable declared in the header of my class.

the instruction prepareToPlay should resolve your problem!