2016-07-29 87 views
1

我试图记录用户的声音,并将其更改为文本数据。我使用AVAudioRecorder录制声音和SpeechKit,将其更改为包含在iOS 10中的文本。当用户触摸按钮记录时,按钮被触摸时开始并停止。但是,当我使用do catch语法初始化AVAudioRecorder时,发生错误并失败。 我添加了相应的框架(Speech,AVFoundation)。Xcode 8.0 beta3 AVAudioRecorder(网址,设置)错误捕获 - “错误域= NSOSStatusErrorDomain代码= -50”(空)“”

import UIKit 
import Speech 
import AVFoundation 

class SearchViewController: UIViewController, CLLocationManagerDelegate, AVAudioRecorderDelegate { 

    var audioRecorder = AVAudioRecorder() 

    let recordSettings = [AVSampleRateKey : String(NSNumber(value: Float(44100.0))), 
         AVFormatIDKey : String(kAudioFileCAFType), 
         AVNumberOfChannelsKey : String(NSNumber(value: 2))] 

@IBAction func recordButtonDown(_ sender: AnyObject) { 

    print("recordButtonDown") 

    self.audioPlayer.play() 
    sleep(1) 

    let fileManager = FileManager.default 
    let paths = fileManager.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask) 
    var audioURL = paths[0] as NSURL 
    audioURL = audioURL.appendingPathComponent("soundForMapSearch.caf", isDirectory: false)! 

    do { 
     self.audioRecorder = try AVAudioRecorder(url: soundFileURL as URL, settings: self.recordSettings) 
     self.audioRecorder.delegate = self 
     self.audioRecorder.prepareToRecord() 
     self.audioRecorder.record() 
    } catch (let error) { 
     print("Error: \(error)") 
    } 
} 

@IBAction func recordButtonUp(_ sender: AnyObject) { 
    self.audioRecorder.stop() 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    do { 
    try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord) 
    try audioSession.setActive(true) 
    audioSession.requestRecordPermission({ (recordPermission) in 
     }) 
    } catch { 
     print("record initiallizing failed") 
    } 

就行

self.audioRecorder = try AVAudioRecorder(url: soundFileURL as URL, settings: self.recordSettings) 

错误发生在捕获

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

被打印。我搜索了这个错误,代码= -50意味着NSURL对象是无效的。我怎样才能解决这个错误?

+0

你不是应该使用'audioURL',而不是这个'soundFileURL'我们一无所知? – Moritz

回答

1

我的工作代码

 let recordingName = "recording1" + ".m4a" 
     let pathArray = [dirPath, recordingName] 
     print(pathArray) 
     let filePath = NSURL.fileURL(withPathComponents: pathArray) 
     print(filePath) 


     do{ 


     let session = AVAudioSession.sharedInstance() 
     try! session.setCategory(AVAudioSessionCategoryPlayAndRecord) 


    } catch { 

    assertionFailure("AVAudioSession setup error: \(error)") 
    } 

     let recordSettings: [String: AnyObject] = [ 
      AVFormatIDKey: NSNumber(value: kAudioFormatMPEG4AAC), 
      AVSampleRateKey: 44100.0, 
      AVNumberOfChannelsKey: 1, 
      ] 

     try! audioRecorder = AVAudioRecorder(url: filePath!, settings: recordSettings) 


     audioRecorder.delegate = self 
     audioRecorder.isMeteringEnabled = true 
     audioRecorder.prepareToRecord() 
     audioRecorder.record() 
+0

谢谢。我的recordSetting无效,所以我修改了它。 –

+0

我使用[String:Any]作为recordSettings。这是我的一个问题。 [String:AnyObject]适用于我。 –