【问题标题】:AVAudioSession endInterruption() returns an NSOSStatusErrorDomainAVAudioSession endInterruption() 返回一个 NSOSStatusErrorDomain
【发布时间】:2011-02-22 20:44:46
【问题描述】:

我从几个小时前开始尝试解决一个 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 中提取的。 奇怪的是样本运行良好!

你们能找出问题所在吗?

谢谢。

【问题讨论】:

    标签: iphone ios4 avaudiosession


    【解决方案1】:

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

    我实现了功能:

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

    它被初始化为:

    AudioSessionInitialize (NULL, NULL, interruptionListenerCallback, self);
    

    在我的 -(id) init 方法中。

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

    【讨论】:

      【解决方案2】:

      “如果此委托方法在其标志参数中接收到 AVAudioSessionInterruptionFlags_ShouldResume 常量,则音频会话立即可以使用。”

      您没有正确处理回调。当您收到 AVAudioSessionInterruptionFlags_ShouldResume 时,您的音频会话已准备好使用。当你得到一个不同的标志时,你需要调用 setActive。

      希望对你有帮助……

      【讨论】:

        【解决方案3】:

        使用@Trinca 回答我在“ARC'ed”项目中所做的事情:

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

        是否 self.player 是您传递给回调函数的播放器实例。

        然后实现回调:

        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;
           }
        
        }
        

        祝你好运

        沙尼

        【讨论】:

          【解决方案4】:

          如果是 voip,则使用 audiosession.setmode

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-27
            • 1970-01-01
            • 2021-05-12
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多