gpt4 book ai didi

iphone - AVAudioSession endInterruption() 返回 NSOSStatusErrorDomain

转载 作者:行者123 更新时间:2023-12-03 20:34:21 26 4
gpt4 key购买 nike

我从几个小时前就开始尝试解决 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 以检查音频代码结果,但没有任何与此数字相关的内容。

我的初始化 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];

}

以及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 中提取的。奇怪的是,该示例运行良好!

你们能找出问题所在吗?

谢谢。

最佳答案

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

我实现了这个功能:

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

它的初始化是:

AudioSessionInitialize (NULL, NULL, interruptionListenerCallback, self);

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

希望对你也有帮助。最好的。

关于iphone - AVAudioSession endInterruption() 返回 NSOSStatusErrorDomain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5083752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com