简体   繁体   中英

How to save recorded audio iOS?

I am developing an application in which audio is being recorded and being transcribed to text. I am using the Speechkit provided by Nuance Developers.

The functions I am adding are:

  • Save the recorded audio file to persistent memory
  • Display the audio files in a table view
  • Load the saved audio files later
  • Play the audio files

How do I save the audio files to persistent storage?

Here's the code : https://gist.github.com/buildFlash/48d143217b721823ff4c3c03a925ba55

When you record audio with AVAudioRecorder then you have to pass path as url of the location where you are storing your audio. so by defaul it's store audio at that location.

for example,

 var audioSession:AVAudioSession = AVAudioSession.sharedInstance()
audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
audioSession.setActive(true, error: nil)

var documents: AnyObject = NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory.DocumentDirectory,  NSSearchPathDomainMask.UserDomainMask, true)[0]
var str =  documents.stringByAppendingPathComponent("myRecording1.caf")
var url = NSURL.fileURLWithPath(str as String)

var recordSettings = [AVFormatIDKey:kAudioFormatAppleIMA4,
    AVSampleRateKey:44100.0,
    AVNumberOfChannelsKey:2,AVEncoderBitRateKey:12800,
    AVLinearPCMBitDepthKey:16,
    AVEncoderAudioQualityKey:AVAudioQuality.Max.rawValue

]

println("url : \(url)")
var error: NSError?

audioRecorder = AVAudioRecorder(URL:url, settings: recordSettings, error: &error)
if let e = error {
    println(e.localizedDescription)
} else {

    audioRecorder.record()
}

So, here url is the location where your audio is stored and you can use that same url to play that audio. and you can get that file from url or path as data if you want to send it to server.

So, if you are using third party library then check that where it is storing audio and you can get it from there or it should have some method to get the location of it.

PS : there is no need to use third party library to record audio because you can easly manage it via AVAudioRecorder and AVAudioPlayer (for playing audio from url). Inshort if you are recording audio then you definitely parallel storing it also!

You can refer Ravi shankar's tutorial also

Reference : this so post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM