2017-06-03 4 views
0

私はAVAudioRecorderを使用して人間の声を録音しています。次のように私のコードは次のとおりです。AVAudioRecorder never records

// 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)") 
} 

私はaudioRecorderEncodeErrorDidOccur関数が呼び出されることは決してありませんが、audioRecorderDidFinishRecording機能はありませんが、flagがあるAVAudioRecorderに停止を呼び出した後、常にfalse。それは私がI上記のコードを使って録音する場合"RECORDED AUDIO SUCCESSFULLY false"

QUESTION

を出力し、指定された場所で私のドキュメントディレクトリにファイルを保存しません。しかし、このファイルは私がプレイできるものではありません。拡張子を.aacと指定したので、オーディオファイルではなくテキストファイルに書き出します。

なぜAVAudioRecorderはオーディオを録音しませんか?そして私はそれをどうやって行うのですか?

答えて

0

問題がstopRecording()機能でラインとありました。 stop()への呼び出しの下で、私は即座にAVAudioRecorderインスタンスをnilに割り当てています。これにより、.aacファイルを作成するための後処理が完了する前にAVAudioRecorderの割り当てを解除して終了します。

0

これは私がそれをやった方法です、最初のインポート

AVFoundation

とあなたのViewControllerにAVAudioRecorderDelegateを追加します。

class RecordViewController: UIViewController, AVAudioRecorderDelegate 

を、その後AVAudioRecorderのグローバルインスタンスを作成します。

var audioRecorder : AVAudioRecorder! 

は、その後、私は録音を開始録音ボタン作成:dirPathの

@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() 
} 

を画像が保存されるディレクトリを検索します。

recordingName

残りはかなり自明です最終的な場所のためのディレクトリとrecordingNameを組み合わせた実際の記録されたファイル

ファイルパスの名前を設定します。これはあなたがオーディオを録音する方法を解決する必要があり

@IBAction func pauseButton(_ sender: Any) { 

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

はその後単純である一時停止ボタンを作成します。

0

URLに「aac」という拡張子が付いていることを確認した場合は、レコーダーでstop()を呼び出すことを忘れてしまった可能性があります。これにより、ファイナライズされていないファイルが作成されます。

また、エラーをcatchブロックで出力します。

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