我正在开展一个与AVFoundation合作的学习项目,负责录制和操作音频的播放。该应用程序在模拟器上运行良好,但在设备上运行时崩溃。
请注意,它不会立即崩溃。当我试图操纵要播放的声音的音高时,它只会崩溃。
此外,如果我只是尝试播放录制的音频,它将无法正常工作,但它也不会崩溃。
如果你想/可以帮助我,整个项目都在github上,https://github.com/pablomarques/Voice-FX
[项目在XCode 6.4上运行,设备是运行8.4的iPhone 6]
提前感谢一大堆。
编辑:在这里添加一些代码,以节省人们必须通过回购的麻烦(如果你想要复制它将保留回购)
它在模拟器上运行得很好,它正在我的头脑中。:(
import UIKit
import AVFoundation
class PlaySoundsViewController: UIViewController {
var audioPlayer:AVAudioPlayer!
var receivedAudio:AudioRecording!
var audioEngine:AVAudioEngine!
var audioFile:AVAudioFile!
override func viewDidLoad() {
super.viewDidLoad()
if let path = receivedAudio.filePathURL {
audioPlayer = AVAudioPlayer(contentsOfURL: path, error: nil)
audioPlayer.enableRate = true
audioEngine = AVAudioEngine()
audioFile = AVAudioFile(forReading: path, error: nil)
println(path)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playSlowSound(sender: AnyObject) {
playSound(0.35)
println("clicking works")
}
@IBAction func playFastSound(sender: AnyObject) {
playSound(3.0)
}
@IBAction func chipMunkSound(sender: AnyObject) {
playAudioWithVariablePitch(1000.0)
}
@IBAction func vaderSound(sender: AnyObject) {
playAudioWithVariablePitch(-1000.0)
}
@IBAction func stopSounds(sender: AnyObject) {
audioPlayer.stop()
audioPlayer.currentTime = 0
}
func playSound(soundSpeed: Float) {
audioPlayer.stop()
audioPlayer.currentTime = 0
audioPlayer.rate = soundSpeed
audioPlayer.prepareToPlay()
audioPlayer.play()
println("playing")
}
func playAudioWithVariablePitch(pitch: Float){
audioPlayer.stop()
audioEngine.stop()
audioEngine.reset()
var audioPlayerNode = AVAudioPlayerNode()
audioEngine.attachNode(audioPlayerNode)
var changePitchEffect = AVAudioUnitTimePitch()
changePitchEffect.pitch = pitch
audioEngine.attachNode(changePitchEffect)
audioEngine.connect(audioPlayerNode, to: changePitchEffect, format: nil)
audioEngine.connect(changePitchEffect, to: audioEngine.outputNode, format: nil)
audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
audioEngine.startAndReturnError(nil)
audioPlayerNode.play()
}
它显示了我的AppDelegate.swift文件中的绿色错误断点,这很奇怪。
class AppDelegate: UIResponder, UIApplicationDelegate {
这是我收到的错误消息:
2015-08-20 05:32:55.506 Voice FX [2265:548015] 05:32:55.506错误:[0x19bb13310]> aurioc> 806:失败:-10851(启用2,outf< 2 ch,0 Hz,Float32,非inter> inf< 2 ch,0 Hz,Float32,非inter>) 2015-08-20 05:32:55.509 Voice FX [2265:548015] 05:32:55.508 ERROR:[0x19bb13310]> aurioc> 806:失败:-10851(启用2,outf< 2 ch,44100 Hz,Float32,非inter> inf< 2 ch,0 Hz,Float32,非inter>) 2015-08-20 05:32:55.512 Voice FX [2265:548015] 05:32:55.512 ERROR:[0x19bb13310]> aurioc> 806:失败:-10851(启用2,outf< 2 ch,44100 Hz,Float32,非inter> inf< 2 ch,0 Hz,Float32,非inter>) 2015-08-20 05:32:55.513 Voice FX [2265:548015] 05:32:55.512错误:[0x19bb13310] AVAudioEngineGraph.mm:2417:PerformCommand:error -10851 2015-08-20 05:32:55.517 Voice FX [2265:548015] *由于未被捕获的异常终止应用程序' com.apple.coreaudio.avfaudio',原因:'错误 - 10851' * 第一次抛出调用堆栈: (0x18563022c 0x1972fc0e4 0x1856300ec 0x183f6e008 0x183fa07ac 0x183fa8a54 0x183fa99dc 0x183f9d9b4 0x183f9da94 0x183f9da94 0x183f9e9f4 0x183fa2140 0x183f9292c 0x183f91f98 0x183f91f28 0x10006c03c 0x10006b20c 0x10006b264 0x18a0e11ec 0x18a0ca2c8 0x18a0e0b88 0x18a0e0814 0x18a0d9d50 0x18a0acf74 0x18a34e124 0x18a0ab488 0x1855e7f8c 0x1855e7230 0x1855e52e0 0x185510f74 0x18ef6b6fc 0x18a112d94 0x10006f3f8 0x1979a6a08) libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)bt
答案 0 :(得分:1)
当我使用较旧的AVAudioSession(用于录制声音文件)功能和新的AVAudioEngine(用于应用音高变换)时,我刚刚遇到此问题。
错误-10851是kAudioUnitErr_InvalidPropertyValue,如此处所示https://developer.apple.com/library/ios/documentation/AudioUnit/Reference/AUComponentServicesReference/
我现在的hacky修复只是在我即将使用AudioEngine时停止我的会话
即
self.avSession?.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
self.avSession?.setActive(false, error: nil)
我仍然收到以下错误
AVAudioSession.mm:646: -[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.
但是语音音调部分正在运行,我的应用程序不再崩溃。所以我猜这只是整理我如何使用两个AVAudio工具的问题。