首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAudioConverter.convertToBuffer抛出错误代码-50

AVAudioConverter.convertToBuffer抛出错误代码-50
EN

Stack Overflow用户
提问于 2016-09-15 04:04:41
回答 1查看 1.2K关注 0票数 3

我需要将一个有2个音频通道录制的.wav文件转换成一个只有1个通道的.wav,并将位深度从32减少到16。我一直在尝试使用AVAudioConverter.convertToBuffer,但是转换抛出了一个错误:Error Domain=NSOSStatusErrorDomain Code=-50 "(null)"

基本上,唯一真正需要改变的是将音频剥离到单声道,以及位深度。我从一个不同的工具获取这些文件,所以我不能简单地改变记录文件的方式。

我在处理音频方面不是很在行,我有点困惑。我正在编写的代码如下--我有什么地方遗漏了吗?

代码语言:javascript
复制
let inAudioFileURL:NSURL = <url_to_wav_file>

var inAudioFile:AVAudioFile?
do
{
    inAudioFile = try AVAudioFile(forReading: inAudioFileURL)
}
catch let error
{
    print ("error: \(error)")
}

let inAudioFormat:AVAudioFormat = inAudioFile!.processingFormat
let inFrameCount:UInt32 = UInt32(inAudioFile!.length)

let inAudioBuffer:AVAudioPCMBuffer = AVAudioPCMBuffer(PCMFormat: inAudioFormat, frameCapacity: inFrameCount)

do
{
    try inAudioFile!.readIntoBuffer(inAudioBuffer)
}
catch let error
{
    print ("readError: \(error)")
}

let startFormat:AVAudioFormat = AVAudioFormat.init(settings: inAudioFile!.processingFormat.settings)
print ("startFormat: \(startFormat.settings)")

var endFormatSettings = startFormat.settings
endFormatSettings[AVLinearPCMBitDepthKey] = 16
endFormatSettings[AVNumberOfChannelsKey] = 1
endFormatSettings[AVEncoderAudioQualityKey] = AVAudioQuality.Medium.rawValue
print ("endFormatSettings: \(endFormatSettings)")


let endFormat:AVAudioFormat = AVAudioFormat.init(settings: endFormatSettings)
let outBuffer = AVAudioPCMBuffer(PCMFormat: endFormat, frameCapacity: inFrameCount)

let avConverter:AVAudioConverter = AVAudioConverter.init(fromFormat: startFormat, toFormat: endFormat)

do
{
    try avConverter.convertToBuffer(outBuffer, fromBuffer: inAudioBuffer)
}
catch let error
{
    print ("avconverterError: \(error)")
}

至于输出:

代码语言:javascript
复制
startFormat: 
  ["AVSampleRateKey": 16000,
  "AVLinearPCMBitDepthKey": 32,
  "AVLinearPCMIsFloatKey": 1,
  "AVNumberOfChannelsKey": 2,
  "AVFormatIDKey": 1819304813,
  "AVLinearPCMIsNonInterleaved": 0,
  "AVLinearPCMIsBigEndianKey": 0]

endFormatSettings:
["AVSampleRateKey": 16000,
"AVLinearPCMBitDepthKey": 16,
"AVLinearPCMIsFloatKey": 1,
"AVNumberOfChannelsKey": 1,
"AVFormatIDKey": 1819304813,
"AVLinearPCMIsNonInterleaved": 0,
"AVLinearPCMIsBigEndianKey": 0,
"AVEncoderQualityKey": 64]

avconverterError: Error Domain=NSOSStatusErrorDomain Code=-50 "(null)"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-15 12:18:35

我不是百分之百确定为什么会这样,但我找到了一个解决方案,让它为我工作,所以我是如何理解这个问题的。我通过尝试使用备用convert(to:error:withInputFrom:)方法找到了这个解决方案。使用它会给我一个不同的错误:

代码语言:javascript
复制
`ERROR:    AVAudioConverter.mm:526: FillComplexProc: required condition is false: [impl->_inputBufferReceived.format isEqual: impl->_inputFormat]`

这个问题是在我设置AVAudioConverter的那一行中引起的

代码语言:javascript
复制
let avConverter:AVAudioConverter = AVAudioConverter.init(fromFormat: startFormat, toFormat: endFormat)

音频转换器似乎希望使用输入缓冲区正在使用的相同AVAudioFormat,而不是使用基于原始设置的副本。一旦我用inAudioFormat替换了startFormatconvert(to:error:withInputFrom:)错误就被消除了,事情就像预期的那样工作了。然后,我能够回到使用更简单的convert(to:fromBuffer:)方法,我正在处理的原始错误也消失了。

简单地说,设置转换器的代码行现在看起来像这样:

代码语言:javascript
复制
let avConverter:AVAudioConverter = AVAudioConverter.init(fromFormat: inAudioFormat, toFormat: endFormat)

至于缺乏关于如何使用AVAudioConverter的文档,我不知道为什么API参考几乎没有。相反,在Xcode中,按住cmd键并单击代码中的AVAudioConverter可以转到它的头文件。这里有大量的评论和信息。不是完整的示例代码或任何东西,但它至少是一些东西。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39498793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档