我正在使用AUAudioUnit播放应用程序正在从服务器流式传输的音频。我的代码在前台工作正常。但是,当我为该应用程序设置背景时,它将无法播放音频。我收到以下错误。
[[aurioc] AURemoteIO.cpp:1590:Start:AUIOClient_StartIO失败(561145187)
错误代码561145187表示AVAudioSessionErrorCodeCannotStartRecording
[此错误类型通常在应用开始混合录制时发生从后台运行,并且未配置为应用间音频应用。
这是我在Swift中设置AVAudioSession的方式:
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .voiceChat, options: [.allowBluetooth])
这是我在Objective-C中设置AUAudioUnit的方式:
AudioComponentDescription description;
description.componentType = kAudioUnitType_Output;
description.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
description.componentManufacturer = kAudioUnitManufacturer_Apple;
description.componentFlags = 0;
description.componentFlagsMask = 0;
self.format = [[AVAudioFormat alloc] initWithSettings: @{
AVFormatIDKey: @(kAudioFormatLinearPCM),
AVSampleRateKey: @16000,
AVNumberOfChannelsKey: @1,
AVLinearPCMIsFloatKey: @NO,
AVLinearPCMBitDepthKey: @16,
AVLinearPCMIsBigEndianKey: @NO
}];
self.audioUnit = [[AUAudioUnit alloc] initWithComponentDescription:description options:0 error:nil];
[当我调用startHardwareAndReturnError
方法时,即是我收到该错误消息。
[self.unit startHardwareAndReturnError:outError];
我已经设置了背景模式-音频功能。而且我很确定我设置了AVAudioSession,因此它是不可混合的。我什至没有录音。我只是想播放音频。我还想念什么?
要在后台录制音频,您必须在前台启动音频单元。然后,音频单元将继续在后台运行。如果没有任何数据可播放,则可以播放静音。
如果您不录制音频,请不要使用playAndRecord会话类型。请改用仅播放会话类型之一。