简体   繁体   中英

How to convert .caf Audio file into .mp4 file in swift

I'm recording audio using device microphone with AVAudioRecorder which return file in .caf format that is playable only in Apple devices but not on Android devices. Since Apple is not supporting .mp3 file so I want to convert it in .mp4 format before uploading to server. Is .mp4 is good for audio only? Can I convert it with AVAssetExportSession ?

Following is audio recorder code:

func setupAudioRecorder ()
    {

    let fileMgr = FileManager.default
    let dirPaths = fileMgr.urls(for:.documentDirectory,
                                in:.userDomainMask)

    let soundFileURL = dirPaths[0].appendingPathComponent("myaudio.caf")

    let recordSettings =
        [AVEncoderAudioQualityKey: AVAudioQuality.min.rawValue,
         AVEncoderBitRateKey: 16,
         AVNumberOfChannelsKey: 2,
         AVSampleRateKey: 44100.0] as [String : Any]

    do {
        try audioSession.setCategory(
            AVAudioSessionCategoryPlayAndRecord)
    } catch let error as NSError {
        print("audioSession error: \(error.localizedDescription)")
    }

    do {
        try audioRecorder = AVAudioRecorder(url: soundFileURL,
                                            settings: recordSettings as [String : AnyObject])
        audioRecorder?.prepareToRecord()
    } catch let error as NSError {
        print("audioSession error: \(error.localizedDescription)")
    }
}

After lot of search i am able to convert .caf into .mp4 using this piece of code

    let audioURL = ".caf audio file url"

    let fileMgr = FileManager.default

    let dirPaths = fileMgr.urls(for: .documentDirectory,
                                in: .userDomainMask)

    let outputUrl = dirPaths[0].appendingPathComponent("audiosound.mp4")

    let asset = AVAsset.init(url: audioURL)

    let exportSession = AVAssetExportSession.init(asset: asset, presetName: AVAssetExportPresetHighestQuality)

    // remove file if already exits
    let fileManager = FileManager.default
    do{
        try? fileManager.removeItem(at: outputUrl)

    }catch{
        print("can't")
    }


    exportSession?.outputFileType = AVFileTypeMPEG4

    exportSession?.outputURL = outputUrl

    exportSession?.metadata = asset.metadata

    exportSession?.exportAsynchronously(completionHandler: {
        if (exportSession?.status == .completed)
        {
            print("AV export succeeded.")

           // outputUrl to post Audio on server

        }
        else if (exportSession?.status == .cancelled)
        {
            print("AV export cancelled.")
        }
        else
        {
            print ("Error is \(String(describing: exportSession?.error))")

        }
    })

You can record in MPEG4 AAC directly without addition step for converting. Use AVFormatIDKey with kAudioFormatMPEG4AAC value and reduce sample rate to 8000 or 16000.

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