2017-04-30 1 views
1

を記録した後にファイルのパスを取得することができませんが、私はオーディオファイルを作るために使用するコードです、すべてがここで正常に動作しているようだ:録音ファイル、ここで

func startRecording() { 
    let audioFilename = getDocumentsDirectory().appendingPathComponent("Meeting_Audio.m4a") 

    let settings = [ 
     AVFormatIDKey: Int(kAudioFormatMPEG4AAC), 
     AVSampleRateKey: 12000, 
     AVNumberOfChannelsKey: 1, 
     AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue 
    ] 

    do { 
     audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings) 
     audioRecorder.delegate = self 
     audioRecorder.record() 

    } catch { 
     finishRecording(success: false) 
    } 
} 

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) { 
    makeFileData() 
} 


@IBAction func onEndMeetingButtonClicked(_ sender: UIButton) { 
    finishRecording(success: true) 
} 

func finishRecording(success: Bool) { 
    audioRecorder.stop() 
    audioRecorder = nil 

    if success { 
     print("recording succeeded :)") 
    } else { 
     print("recording failed :(") 
    } 
} 

そして、ここでは単純化されたバージョンですまだ問題を再現している私の機能です。私はこの機能をaudioRecorderDidFinishRecordingから呼び出しています。ファイルパスを取得できないようです。

func makeFileData() { 

    if let filePath = Bundle.main.path(forResource: "Meeting_Audio", ofType: "m4a"){ 
     print("File path loaded.") 

     if let fileData = NSData(contentsOfFile: filePath) { 
      print("File data loaded.") 
     } 
    } 
} 

答えて

0

あなたが一つの場所にファイルを書いている:

// Documents directory! 
let audioFilename = getDocumentsDirectory().appendingPathComponent("Meeting_Audio.m4a") 

と別からそれを読んで:

// app bundle! 
let filePath = Bundle.main.path(forResource: "Meeting_Audio", ofType: "m4a") 

はあなたに手紙を書いた場所からファイルを読み込む試してみてください、のようなものをこれは:

if let fileUrl = Bundle.main.url(forResource: "Meeting_Audio", withExtension: "m4a"){ 
    print("File path loaded.") 

    if let fileData = NSData(contentsOf: fileUrl) { 
     print("File data loaded.") 
    } 
} 
関連する問題