AVAudioSession endInterruption()返回NSOSStatusErrorDomain [英] AVAudioSession endInterruption() returns an NSOSStatusErrorDomain

查看:210
本文介绍了AVAudioSession endInterruption()返回NSOSStatusErrorDomain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决一个AVAudioSession问题,因为很多个小时前并没有成功!!

I'm trying to solve an AVAudioSession problem since many hours ago and didn't get success!!

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

I found lots of guys talking about problems with endInterruption but none of them talking 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.)"

我尝试转换此代码到ASCII检查音频代码结果,但没有任何与此号码相关的信息。

I tried converting this code to ASCII to check the Audio Code Results, but there is nothing related to this number.

我的代码初始化:

- (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的代码方法:

And the code of 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]; 

        }
    }
}

}

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

A big part of this code was extracted from Apple Mixer Host Sample. And it's weird that the sample works fine!

你们能弄明白什么是错的吗?

Can you guys figure out what is wrong?

谢谢。

推荐答案

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

I solved the problem changing the way to design my audio code. Instead of use AVAudioSession and its delegate methods, I change to the C style to work with audio.

我实现了这个功能:

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

它初始化为:

AudioSessionInitialize (NULL, NULL, interruptionListenerCallback, self);

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

inside my -(id) init method.

希望它也能帮到你。
Best。

Hope it helps you also. Best.

这篇关于AVAudioSession endInterruption()返回NSOSStatusErrorDomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆