Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中创建录音_Swift_Audio_Avfoundation_Avaudiorecorder_Recording - Fatal编程技术网

在Swift中创建录音

在Swift中创建录音,swift,audio,avfoundation,avaudiorecorder,recording,Swift,Audio,Avfoundation,Avaudiorecorder,Recording,我开始自学Swift,通过创建一个让用户录制语音备忘录的应用程序。以前关于这个主题的许多问题似乎都使用了过时的Swift版本,所以我想我应该向社区寻求帮助 问题似乎出在playAudio()函数或viewDidLoad()函数中,我在其中设置了录制URL(或两者都有)。如果我能提供更多信息,请告诉我 @IBAction func playAudio(sender: AnyObject) throws { if audioRecorder?.recording == false {

我开始自学Swift,通过创建一个让用户录制语音备忘录的应用程序。以前关于这个主题的许多问题似乎都使用了过时的Swift版本,所以我想我应该向社区寻求帮助

问题似乎出在playAudio()函数或viewDidLoad()函数中,我在其中设置了录制URL(或两者都有)。如果我能提供更多信息,请告诉我

@IBAction func playAudio(sender: AnyObject) throws {

    if audioRecorder?.recording == false {

        stopButton.enabled = true
        recordButton.enabled = false
            let path = NSBundle.mainBundle().pathForResource("audioFile", ofType: "m4a")
            let fileURL = try NSURL(fileURLWithPath: path!)
            audioPlayer = try AVAudioPlayer(contentsOfURL: fileURL, fileTypeHint: nil)
            audioPlayer.prepareToPlay()
            audioPlayer.delegate = self
            print("here you go!")
    }
}
重写func viewDidLoad(){


你得到了什么错误?我猜没有找到文件,是真的吗?这就是我看到的:///var/mobile/Containers/Data/Application/389A4125-0C7C-4AF9-8B5C-225B6A3425A7/sound.caf 2015-11-17 23:31:56.854 AppName[7076:3307927]-[AppName.ViewController playAudio:]:未识别的选择器发送到实例0x15564e430 2015-11-17 23:31:56.859 AppName[7076:3307927]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[AppName.ViewController播放音频:]:无法识别的选择器发送到实例0x15564e430'***第一次抛出调用堆栈:…libc++abi.dylib:以NSException类型的未捕获异常终止。我看不出您的代码有任何错误。您似乎在添加目标,但未定义函数。请在代码中检查这一点。这是我唯一能想到的检查方法出局:Peyman,谢谢你的帮助。不幸的是,你发送的链接不适用于我的情况,因为我已经定义了我调用的所有函数。你会遇到什么错误?我猜文件没有找到,是真的吗?我看到的是:///var/mobile/Containers/Data/Application/389A4125-0C7C-4AF9-8B5C-225B6A3425A7/sound.caf 2015-11-17 23:31:56.854 AppName[7076:3307927]-[AppName.ViewController播放音频:]:发送到实例0x15564e430 2015-11-17 23:31:56.859 AppName[7076:3307927]***由于未捕获异常“NSInvalidArgumentException”终止应用,原因:'-[AppName.ViewController播放音频:]:无法识别的选择器发送到实例0x15564e430'***第一次抛出调用堆栈:…libc++abi.dylib:以NSException类型的未捕获异常终止。我看不出您的代码有任何错误。您似乎在添加目标,但未定义函数。请在代码中检查这一点。这是我唯一能想到的检查方法佩曼,谢谢你的帮助。不幸的是,你发送的链接不适用于我的情况,因为我已经定义了我调用的所有函数。
    super.viewDidLoad()

    playButton.enabled = false
    stopButton.enabled = false

    do {
        let dirPaths =
            NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

        let baseURL = NSURL(string: dirPaths[0])
        let soundFileURL = NSURL(string:"sound.caf", relativeToURL: baseURL)
        let absoluteURL = soundFileURL!.absoluteURL
        let recordSettings = [AVEncoderAudioQualityKey: AVAudioQuality.Min.rawValue, AVEncoderBitRateKey: 16, AVNumberOfChannelsKey: 2, AVSampleRateKey: 44100.0]
        var error: NSError?

        let audioSession = AVAudioSession.sharedInstance()
        try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [])

        if let err = error {
            print("audioSession error: \(err.localizedDescription)")
        }

        try audioRecorder = AVAudioRecorder(URL: absoluteURL, settings: recordSettings as! [NSObject : AnyObject] as! [String: AnyObject])
        print(audioRecorder!.url)
        if let err = error {
            print("audioSession error: \(err.localizedDescription)")
        } else {
            audioRecorder?.prepareToRecord()
        }

    } catch {
        print("catching error!")
    }