【问题标题】:How to convert .caf Audio file into .mp4 file in swift如何在 swift 中将 .caf 音频文件转换为 .mp4 文件
【发布时间】:2017-08-31 06:49:35
【问题描述】:

我正在使用带有AVAudioRecorder 的设备麦克风录制音频,它返回.caf 格式的文件,该文件只能在Apple 设备上播放,而不能在Android 设备上播放。由于 Apple 不支持 .mp3 文件,所以我想在上传到服务器之前将其转换为 .mp4 格式。 .mp4 是否仅适用于音频?我可以用AVAssetExportSession 转换它吗?

以下是录音机代码:

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)")
    }
}

【问题讨论】:

    标签: ios swift xcode avaudiorecorder avassetexportsession


    【解决方案1】:

    经过大量搜索,我可以使用这段代码将 .caf 转换为 .mp4

        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))")
    
            }
        })
    

    【讨论】:

      【解决方案2】:

      您可以直接在 MPEG4 AAC 中录制,无需额外的转换步骤。使用 AVFormatIDKeykAudioFormatMPEG4AAC 值并将采样率降低到 8000 或 16000。

      【讨论】:

      • 感谢您的帮助,但我也需要 .caf,因为在上传到服务器之前,我需要使用 AVAudioPlayer 播放该音频。
      • 您可以使用 AVAudioPlayer 播放 AAC。
      • 我的代码有:let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44000 etc....)。您是说将 commonFormat 更改为 .kAudioFormatMPEG4AAC 吗?或者还有其他步骤吗?
      猜你喜欢
      • 2013-09-19
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 2011-12-07
      • 2016-03-23
      • 1970-01-01
      • 2020-04-29
      相关资源
      最近更新 更多