Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert .caf file to .mp3 file?

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 ?

like image 764
Ankahathara Avatar asked Oct 19 '22 20:10

Ankahathara


1 Answers

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?

like image 74
Igor Avatar answered Nov 11 '22 13:11

Igor