Swift - save audio recordings

1.4k Views Asked by At

It would be much appreciated if someone can help me with the following.

I want to record audio and then display it on a table view.

I have got the recording part

  func setupRecorder() {
    var format = NSDateFormatter()
    format.dateFormat="yyyy-MM-dd-HH-mm-ss"
    var currentFileName = nameTextfield.text
    println(currentFileName)

    var dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    var docsDir: AnyObject = dirPaths[0]
    var soundFilePath = docsDir.stringByAppendingPathComponent(currentFileName)
    soundFileURL = NSURL(fileURLWithPath: soundFilePath)
    let filemanager = NSFileManager.defaultManager()
    if filemanager.fileExistsAtPath(soundFilePath) {
        // probably won't happen. want to do something about it?
        println("sound exists")
    }

    var recordSettings = [
        AVFormatIDKey: kAudioFormatAppleLossless,
        AVEncoderAudioQualityKey : AVAudioQuality.Max.toRaw(),
        AVEncoderBitRateKey : 320000,
        AVNumberOfChannelsKey: 2,
        AVSampleRateKey : 44100.0
    ]
    var error: NSError?
    recorder = AVAudioRecorder(URL: soundFileURL!, settings: recordSettings, error: &error)
    if let e = error {
        println(e.localizedDescription)
    } else {
        recorder.delegate = self
        recorder.meteringEnabled = true
        recorder.prepareToRecord() // creates/overwrites the file at soundFileURL
    }` 

I am using a model that consists of 2 properties.(Title"string" and media"recording")

Having trouble saving the audio(NSUrl) and displaying it on the tableview.

I am noob as you can see and any help would be great!

Thanks

1

There are 1 best solutions below

0
On

Most likely you'll need the Session created:

    var session=AVAudioSession.sharedInstance()
    session.setCategory(AVAudioSessionCategoryPlayAndRecord,error:nil)