Saving Recorded Audio (Swift)

后端 未结 1 1389
深忆病人
深忆病人 2021-02-04 21:50

I\'m trying to record an audio with AVAudioRecorder and then save it so i can use it later. So far I have Made the Recorder and it does the job well and plays the audio after re

相关标签:
1条回答
  • 2021-02-04 22:16

    I think the file is probably still there when the app quits, the problem is that your viewDidLoad() method immediately calls setupRecorder(), which in turn creates a new AVAudioRecorder using exactly the same filename as last time – overwriting your work.

    To help you re-arrange your code, go to audioRecorderDidFinishRecording() and change print(recordedAudio.title) to print(recorder.url). If you're running in the iOS Simulator that will give you a long path to an exact filename on your OS X disk drive.

    If you browse there using Finder you'll be able to see your "audioFile.m4a" file being created and overwritten again and again, which will let you see exactly when your problem occurs. If you want to see the exact problem, set a breakpoint in your code when you call prepareToPlay(), check your file's size, then press F6 to execute that line of code, then check your file's size again – you should see it being cleared :)

    The solution is probably to generate a new filename every time. You could use NSUUID for that if you wanted:

    let filename = NSUUID().UUIDString + ".m4a"
    

    You might find my tutorial on AVAudioRecorder useful.

    Note: your getCacheDirectory() method is poorly named. It's fetching the documents directory, not the caches directory, which is a bit of a red herring when trying to debug issues like this.

    0 讨论(0)
提交回复
热议问题