例外com.apple.coreaudio.avfaudio原因:错误-50

God*_*her 8 core-audio avfoundation ios avaudiosession swift

当我尝试播放不同音高的音频时,我收到此消息: 在此输入图像描述

我没有成功就用谷歌搜索了那个错误.如果我设置断点,它会在此处停止: 在此输入图像描述

我测试打印所有对象,看看有什么是尼特,但我没有找到任何东西.最神奇的事情是,只发生在我的iphone6 +中,在我测试过的其他手机中没有破坏.然后搜索我调查的项目,添加这个声音效果,这是:https: //github.com/atikur/Pitch-Perfect 如果你运行它,它可以工作,直到你改变...

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, error: &error)
Run Code Online (Sandbox Code Playgroud)

至:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: &error)
Run Code Online (Sandbox Code Playgroud)

然后繁荣(只有真正的设备连接到XCODE,它在模拟器中工作):

2015-03-21 11:56:13.311 Pitch Perfect [1237:607678] 11:56:13.311 ERROR:[0x10320c000] AVAudioFile.mm:496: - [AVAudioFile readIntoBuffer:frameCount:error:]:error -50 2015-03 -21 11:56:13.313 Pitch Perfect [1237:607678] *由于未捕获的异常'com.apple.coreaudio.avfaudio'终止app,原因:'error -50'*第一次抛出调用堆栈:(0x18687a530 0x1978040e4 0x18687a3f0 0x1851ea6c0 0x185232d38 0x1852130f8 0x185212ccc 0x100584fd4 0x100584f94 0x10058fdb8 0x1005882c4 0x1005925d4 0x100592208 0x198037dc8 0x198037d24 0x198034ef8)libc ++ abi.dylib:以NSException类型的未捕获异常终止

真正非常奇怪的是这个截图,由于某些原因,在打印audioEngine后,audioEngine.outputNode变为零? 怪异的bug

小智 0

我遇到了同样的错误...我创建了一个“sound.swift”类,我的视图控制器将实例化该类...我决定简化一切并专注于使声音正常工作。所以我将以下代码放入视图控制器中并且它可以工作:

//获取录音文件

var pitchPlayer = AVAudioPlayerNode()
var timePitch = AVAudioUnitTimePitch()
let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0] as! String

var pathArray = [dirPath, String("son.wav")]

filePath = NSURL.fileURLWithPathComponents(pathArray)

audioFile = AVAudioFile(forReading: filePath.filePathURL, error: nil)

audioEngine = AVAudioEngine()

audioEngine.attachNode(pitchPlayer)
audioEngine.attachNode(timePitch)


//Create a session
var session=AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayAndRecord,error:nil)

//output audio
session.overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error: nil)

audioEngine.connect(pitchPlayer, to: timePitch, format: audioFile.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile.processingFormat)

pitchPlayer.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
audioEngine.startAndReturnError(&audioError)

pitchPlayer.play()
Run Code Online (Sandbox Code Playgroud)