5

I have record some voice in to the Document Folder using following methods

 func record() {
    self.prepareToRecord()
    if let recorder = self.audioRecorder {
        recorder.record()
    }
}

    let recordSettings = [
    AVSampleRateKey : NSNumber(float: Float(44100.0)),
    AVFormatIDKey : NSNumber(int: Int32(kAudioFormatAppleLossless)),
    AVNumberOfChannelsKey : NSNumber(int: 2),
    AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Medium.rawValue)),
    AVEncoderBitRateKey : NSNumber(int: Int32(320000))
]

    func prepareToRecord() {

    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
    let soundFileURL: NSURL? = NSURL.fileURLWithPath("\(documentsPath)/recording.caf")
    print("\(soundFileURL)")

    self.audioRecorder = try! AVAudioRecorder(URL: soundFileURL!, settings: recordSettings)

    if let recorder = self.audioRecorder {
        recorder.prepareToRecord()
    }
}

This method will save the audio file as recording.caf in to the Document Directory, but I want to convert this recording.caf file in to mp3 for further manipulation.

How to convert this .caf file to .mp3 in swift ?

Ankahathara
  • 2,866
  • 2
  • 20
  • 26

2 Answers2

4

mp3 codec is not supported by AVAudioRecorder because of royalty The list of available codecs:

kAudioFormatMPEG4AAC
kAudioFormatAppleLossless
kAudioFormatAppleIMA4
kAudioFormatiLBC
kAudioFormatULaw
kAudioFormatLinearPCM

You can find the details here How do I record audio on iPhone with AVAudioRecorder?

Community
  • 1
  • 1
Igor
  • 1,537
  • 10
  • 12
-1

You would need to make use of the Lame Encoder for this.

Here is the stack overflow link which explains how to do this in swift.

wav to mp3 in swift

Community
  • 1
  • 1
Xcoder
  • 1,762
  • 13
  • 17