使用RPBroadcastSampleHandler将广播保存到应用组容器中

问题描述

我尝试使用应用程序组将视频保存在设备上,但是当我使用组目录时,它不起作用。当我使用videoFileLocation()的第一个变体调用videoWriter.startWriting()时,它将失败,但是当我使用第二个变体-videoWriter开始写入时。组的变体怎么了?

这是我的课程字段:

import ReplayKit

class SampleHandler: rpbroadcastsamplehandler {
    
    let appIdentifier = "group.CY-2"
    let fileManager = FileManager.default
    var isRecording = false
    
    var videoWriterInput: AVAssetWriterInput!
    var audioWriterInput: AVAssetWriterInput!
    var videoWriter: AVAssetWriter!
    
    var sessionAtSourceTime: CMTime!
    
    var outputFileLocation: URL!

这是使用在setUpWriter()中调用的应用程序组的无效代码。结果:

videoWriter.startWriting()=假

videoWriter.status =失败

func videoFileLocation() -> URL {
            let documentsPath = fileManager.containerURL(forSecurityApplicationGroupIdentifier: appIdentifier)!
            let videoOutputUrl = documentsPath.appendingPathComponent("videoFile").appendingPathExtension("mov")
            do {
                if fileManager.fileExists(atPath: videoOutputUrl.path) {
                    try fileManager.removeItem(at: videoOutputUrl)
                }
                 fileManager.createFile(atPath: videoOutputUrl.path,contents: nil,attributes: nil)
            } catch {
                print(error)
            }
            return videoOutputUrl
        }

但这很好用,结果是:

videoWriter.startWriting()= true

videoWriter.status =写作

func videoFileLocation() -> URL {
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0] as Nsstring
    let videoOutputUrl = URL(fileURLWithPath: documentsPath.appendingPathComponent("videoFile")).appendingPathExtension("mov")
    do {
        if fileManager.fileExists(atPath: videoOutputUrl.path) {
            try fileManager.removeItem(at: videoOutputUrl)
        }
    } catch {
        print(error)
    }
    return videoOutputUrl
}

setUpWriter()函数代码

func setUpWriter() {
        do {
            self.outputFileLocation = videoFileLocation()
            videoWriter = try AVAssetWriter(outputURL: outputFileLocation,fileType: .mov)
            
            var status = videoWriter.status
            
            // add video input
            videoWriterInput = AVAssetWriterInput(mediaType: AVMediaType.video,outputSettings: [
                AVVideoCodecKey : AVVideoCodecType.h264,AVVideoWidthKey : 720,AVVideoHeightKey : 1280,AVVideoCompressionPropertiesKey : [
                    AVVideoAverageBitRateKey : 2300000,],])
            
            videoWriterInput.expectsMediaDataInRealTime = true
            
            if videoWriter.canAdd(videoWriterInput) {
                videoWriter.add(videoWriterInput)
                print("video input added")
            } else {
                print("no input added")
            }
            
            // add audio input
            audioWriterInput = AVAssetWriterInput(mediaType: AVMediaType.audio,outputSettings: nil)
            
            audioWriterInput.expectsMediaDataInRealTime = true
            
            if videoWriter.canAdd(audioWriterInput!) {
                videoWriter.add(audioWriterInput!)
                print("audio input added")
            }
            
            let wasstarted = videoWriter.startWriting() //false true
            
            status = videoWriter.status //Failed writing
        } catch let error {
            debugPrint(error.localizedDescription)
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)