2017-04-30 83 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.") 
    } 
}