iOS - 录制 mp4 文件

Posted

技术标签:

【中文标题】iOS - 录制 mp4 文件【英文标题】:iOS - Record an mp4 file 【发布时间】:2019-02-07 12:42:43 【问题描述】:

所以我想在录制时生成一个 mp4 文件,我能做的就是只生成 .m4a 或 .caf。 当我试图生成其他文件时它失败了 使用此代码:

import AVFoundation

class ViewController: UIViewController 
var fileName: String = "audioFile.m4a"

var soundRecorder: AVAudioRecorder?
var soundPlayer: AVAudioPlayer?
var audiosession = AVAudioSession.sharedInstance()

override func viewDidLoad() 
    super.viewDidLoad()
    setUpRecorder()
    playBtn.isEnabled = false



override func viewDidAppear(_ animated: Bool) 
    super.viewDidAppear(animated)
    self.audioSession = AVAudioSession.sharedInstance()

    do 
        try audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: .measurement, options: .defaultToSpeaker)
        try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
     catch 
        print(error)
    



func getDocDirector() -> URL 
    let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    return path[0]


func setUpRecorder() 


    let audioFileName = getDocDirector().appendingPathComponent(fileName)
    let recordSettings = [ AVFormatIDKey: Int(kAudioFormatAppleLossless),
                           AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue,
                           AVEncoderBitRateKey: 192000,
                           AVNumberOfChannelsKey: 1,
    AVSampleRateKey: 441000 ] as [String: Any]

    do 
        soundRecorder = try AVAudioRecorder(url: audioFileName, settings: recordSettings)
        soundRecorder?.delegate = self
        soundRecorder?.prepareToRecord()
     catch 
       print(error)
    


func setUpPlayer() 
    let audioFileName = getDocDirector().appendingPathComponent(fileName)
    do 
        soundPlayer = try AVAudioPlayer(contentsOf: audioFileName)
        soundPlayer?.delegate = self
        soundPlayer?.prepareToPlay()
        soundPlayer?.volume = 1.0
     catch 
        print(error)
    


@IBAction func recordAction(_ sender: Any) 
    if recordBtn.titleLabel?.text == "Record" 
        soundRecorder?.record()
        recordBtn.setTitle("Stop", for: .normal)
        playBtn.isEnabled = false
     else 
        soundRecorder?.stop()
        recordBtn.setTitle("Record", for: .normal)
        playBtn.isEnabled = false
    


@IBAction func playAction(_ sender: Any) 
    if playBtn.titleLabel?.text == "Play" 
        playBtn.setTitle("Stop", for: .normal)
        recordBtn.isEnabled = false
        setUpPlayer()
        soundPlayer?.play()
     else 
        playBtn.setTitle("Play", for: .normal)
        recordBtn.isEnabled = false
    


extension ViewController: AVAudioRecorderDelegate 
 func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) 
    playBtn.isEnabled = true


func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) 
    print(error!)



extension ViewController: AVAudioPlayerDelegate   
 func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) 
    recordBtn.isEnabled = true
    playBtn.setTitle("Play", for: .normal)


所以这一切都很好,但我无法找到编写 mp4 的方法 有没有办法直接写mp4? 我的最终目标是生成碎片化的 mp4 以通过套接字发送...... 很高兴得到建议

【问题讨论】:

尝试将AVFormatIDKeykAudioFormatMPEG4AAC一起使用,并将采样率降低到8000或16000 @BadCodeDeveloper 尝试过...没有成功 【参考方案1】:

将您的设置字典更改为这个:

let recordSettings: [String: Any] = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                                         AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue,
                                         AVNumberOfChannelsKey: 1,
                                         AVEncoderBitRateKey: 16000,
                                         AVSampleRateKey: 16000]

别忘了更改fileName的扩展名:

var fileName: String = "audioFile.mp4"

【讨论】:

完美运行!

以上是关于iOS - 录制 mp4 文件的主要内容,如果未能解决你的问题,请参考以下文章

iOS 录制视频MOV格式转MP4

如何使来自 iOS 应用程序的 MP4 录制视频可在 Chrome 上播放?

iOS 视频录制压缩上传

android中的视频录制格式(.3gp或mp4)?

如何用ffmpeg将rtsp视频流录制成mp4文件?

在播放 iOS 时从 HLS 流(视频)中提取/录制音频