AVAssetWriter无法同时录制音频和视频 | 崩溃
创始人
2024-11-12 20:30:25
0

问题描述: 当使用AVAssetWriter同时录制音频和视频时,应用程序崩溃。

解决方法: 可以使用AVCaptureSession来同时录制音频和视频,避免AVAssetWriter无法处理音频和视频的同时写入。

以下是一个示例代码,展示了如何使用AVCaptureSession来同时录制音频和视频:

import AVFoundation

class Recorder {
    var captureSession: AVCaptureSession?
    var videoOutput: AVCaptureMovieFileOutput?
    var audioOutput: AVCaptureAudioDataOutput?
    
    func startRecording() {
        captureSession = AVCaptureSession()
        
        guard let captureSession = captureSession else { return }
        
        // 添加音频输入
        let audioDevice = AVCaptureDevice.default(for: .audio)
        guard let audioInput = try? AVCaptureDeviceInput(device: audioDevice!) else { return }
        if captureSession.canAddInput(audioInput) {
            captureSession.addInput(audioInput)
        }
        
        // 添加视频输入
        let videoDevice = AVCaptureDevice.default(for: .video)
        guard let videoInput = try? AVCaptureDeviceInput(device: videoDevice!) else { return }
        if captureSession.canAddInput(videoInput) {
            captureSession.addInput(videoInput)
        }
        
        // 添加音频输出
        audioOutput = AVCaptureAudioDataOutput()
        if captureSession.canAddOutput(audioOutput!) {
            captureSession.addOutput(audioOutput!)
        }
        
        // 添加视频输出
        videoOutput = AVCaptureMovieFileOutput()
        if captureSession.canAddOutput(videoOutput!) {
            captureSession.addOutput(videoOutput!)
        }
        
        // 设置录制方向
        let connection = videoOutput?.connection(with: .video)
        connection?.videoOrientation = .portrait
        
        // 开始录制
        let outputPath = NSTemporaryDirectory() + "output.mov"
        let outputURL = URL(fileURLWithPath: outputPath)
        videoOutput?.startRecording(to: outputURL, recordingDelegate: self)
        
        captureSession.startRunning()
    }
    
    func stopRecording() {
        videoOutput?.stopRecording()
        captureSession?.stopRunning()
    }
}

extension Recorder: AVCaptureFileOutputRecordingDelegate {
    func fileOutput(_ output: AVCaptureFileOutput, didStartRecordingTo fileURL: URL, from connections: [AVCaptureConnection]) {
        // 录制开始
    }
    
    func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
        // 录制结束
    }
}

使用上述代码,可以在开始录制时调用startRecording()方法,在结束录制时调用stopRecording()方法。同时录制音频和视频,并且避免了AVAssetWriter无法处理同时写入音频和视频的问题。

相关内容

热门资讯

关于!微信微乐自建房辅助工具,... 关于!微信微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)方法1)微乐家乡麻将自建房辅...
大家学习交流!微信卡五星辅助器... 大家学习交流!微信卡五星辅助器,微乐小程序免费黑科技(开挂自建房)方法1、很好的工具软件,可以解锁游...
三分钟了解!微乐微信自建房辅助... 三分钟了解!微乐微信自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具1、三分钟了解...
一分钟教会你!微乐家乡麻将自建... 一分钟教会你!微乐家乡麻将自建房辅助app,微乐自建房脚本免费入口(开挂自建房)方法1、用户打开应用...
分享实测!微信微乐自建房辅助工... 分享实测!微信微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)器1、起透看视 微信微乐自建房...
重大通报!微乐自建房辅助工具脚... 重大通报!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)app微乐自建房辅助工具脚本破解...
如何分辨真伪!微乐微信自建房辅... 如何分辨真伪!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)技巧1、微乐微信自建房辅...
我来教教你!微乐自建房辅助工具... 我来教教你!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)工具1、玩家可以在微乐家...
重大来袭!微乐自建房辅助工具神... 重大来袭!微乐自建房辅助工具神器,微乐小程序免费黑科技(开挂自建房)器1、游戏颠覆性的策略玩法,独创...
带你了解!微信卡五星辅助器,微... 带你了解!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)方法1、带你了解!微信卡五星辅助器,...