miércoles, 21 de octubre de 2015

How to record audio


  1. Import library AVFoundation

  2. import AVFoundation

  3. Declare global variable with type AVAudioRecorder

  4. var audioRecorder:AVAudioRecorder!

  5. In the funtion to record the audio
    • Declare the directory where the files will be recorded

         let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
    • Declare the filename

         let recordingName = "my_audio.wav"
    • Declare the filePath

         let pathArray = [dirPath, recordingName]
         let filePath = NSURL.fileURLWithPathComponents(pathArray)
    • Set the settings

         let session = AVAudioSession.sharedInstance()
         try! session.setCategory(AVAudioSessionCategoryPlayAndRecord)
    
    
    • Record the audio

         try! audioRecorder = AVAudioRecorder(URL: filePath!, settings: [:])
         audioRecorder.meteringEnabled = true
         audioRecorder.prepareToRecord()
         audioRecorder.record()
4. In the function to stop the record
         audioRecorder.stop()
         let audioSession = AVAudioSession.sharedInstance()
         try! audioSession.setActive(false)
    

No hay comentarios:

Publicar un comentario