2017-06-03 126 views
0

我正在使用AVAudioRecorder來記錄人聲。我的代碼如下:AVAudioRecorder永不記錄

// Property of Class 
var recorder:AVAudioRecorder? 

func recordButtonTapped() { 
    let audioSession = AVAudioSession.sharedInstance() 
    do { 
     try audioSession.setCategory(AVAudioSessionCategoryRecord) 
     try audioSession.setActive(true) 
     if audioSession.recordPermission() != .granted { 
      audioSession.requestRecordPermission({ (success) in 
       self.startRecording() 
      }) 
     } 
     else { 
      self.startRecording() 
     } 

    } catch { 
     print("Unable To Set Category") 
    } 
} 

func startRecording() { 
    // libraryPathWith(media) just gets the path to the documents directory 
    // Like so: Documents/MediaLibrary/Audio/<mediaID>.<mediaExtension> 
    if let path = MMFileManager.libraryPathWith(media: self.media) { 
     isRecording = true 
     do { 
      let settings = [ 
       AVFormatIDKey: Int(kAudioFormatMPEG4AAC), 
       AVSampleRateKey: 44100, 
       AVNumberOfChannelsKey: 1, 
       AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue 
      ] 

      recorder = try AVAudioRecorder(url: path, settings: settings) 

      recorder?.delegate = self 

      if recorder!.prepareToRecord() { 
       recorder?.record() 
      } 
     } 
     catch { 
      isRecording = false 
     } 
    } 
} 

func stopRecording() { 
    self.recordingLabel.text = "Recording Complete" 
    self.recordingLabel.textColor = UIColor.white 
    if let rec = recorder { 
     rec.stop() 
     recorder = nil 
    } 
    isRecording = false 
} 

AVAudioRecorderDelegate

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) { 
    print("RECORDED AUDIO SUCCESSFULLY \(flag)") 
} 
func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) { 
    print("AUDIO RECORDER ERROR \(error?.localizedDescription)") 
} 

後,我呼籲停止對AVAudioRecorder的audioRecorderEncodeErrorDidOccur功能不會被調用,但audioRecorderDidFinishRecording功能的確但flag是總是false。它打印出"RECORDED AUDIO SUCCESSFULLY false"

問題

當我在上面我沒有將文件保存到我的文檔目錄中指定的位置記錄使用的代碼。但是這個文件不是我能玩的東西。它寫入文本文件,而不是音頻文件,因爲我指定的擴展名爲.aac

爲什麼AVAudioRecorder不錄音?我怎樣才能做到這一點?

回答

0

問題同在stopRecording()功能就行了。在撥打stop()的電話下面,我立刻將AVAudioRecorder實例分配給nil。這個釋放和結束AVAudioRecorder之前的後處理創建正宗.aac文件可以完成。

0

這是我做到了,第一次導入

AVFoundation

和AVAudioRecorderDelegate添加到您的ViewController:

class RecordViewController: UIViewController, AVAudioRecorderDelegate 

然後創建AVAudioRecorder的全局實例:

var audioRecorder : AVAudioRecorder! 

然後創建一個啓動錄像的錄製按鈕:

@IBAction func playButton(_ sender: Any) { 

    let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String 
    let recordingName = "voiceRecording.wav" 
    let pathArray = [dirPath, recordingName] 
    let filePath = URL(string: pathArray.joined(separator: "/")) 

    let session = AVAudioSession.sharedInstance() 
    try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker) 

    try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:]) 
    audioRecorder.delegate = self 
    audioRecorder.isMeteringEnabled = true 
    audioRecorder.prepareToRecord() 
    audioRecorder.record() 
} 

dirPath發現該圖像將被存儲在目錄中。

recordingName將設置實際記錄的文件

文件路徑的名稱結合了最後的位置的目錄和recordingName

其餘是相當多的自我解釋。

然後創建一個暫停按鈕,其結構簡單:

@IBAction func pauseButton(_ sender: Any) { 

    audioRecorder.stop() 
    let audioSession = AVAudioSession.sharedInstance() 
    try! audioSession.setActive(false) 
} 

這應該解決如何錄製的聲音。

0

如果您已驗證您的網址具有「aac」擴展名,那麼我懷疑您只是忘記在記錄器上調用stop()。這導致未完成的文件。

還在catch塊中打印您的錯誤。

do{ 
    try throwingFunc() 
} catch { 
    print(error) 
}