自许多小时前以来,我一直在尝试解决AVAudioSession问题,但没有成功!!

我发现很多人都在谈论endInterruption的问题,但是没有一个人在谈论这个错误:

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


我尝试将此代码转换为ASCII以检查音频代码结果,但是与此数字没有任何关系。

我的代码来初始化会话:

- (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];


}

以及endInterruption方法的代码:

- (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];

        }
    }
}


}

此代码的很大一部分是从Apple Mixer Host Sample中提取的。
样本运行正常很奇怪!

你们能弄清楚哪里出了问题吗?

谢谢。

最佳答案

我解决了更改音频代码设计方式的问题。
我改用C样式来处理音频,而不是使用AVAudioSession及其委托方法。

我实现了该功能:

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


并初始化为:

AudioSessionInitialize (NULL, NULL, interruptionListenerCallback, self);


在我的-(id)初始化方法中。

希望它也对您有帮助。
最好。

09-26 07:48