0

我正在制作一个需要使用录制视频和音频的应用程序AVCaptureVideoDataOutputSampleBufferDelegate

我使用的功能是:

    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!)
{
    self.captureQueue.async {
        if !self.isCapturing
        {
            return
        }
        var isVideo = true
        if connection != self.videoConnection
        {
            isVideo = false
        }

        self.encoder!.encodeFrame(sampleBuffer: sampleBuffer, isVideo: isVideo)

    }
}

和编码帧功能:

func encodeFrame(sampleBuffer : CMSampleBuffer , isVideo : Bool)
{

        if (CMSampleBufferDataIsReady(sampleBuffer))
        {
            if self.writer.status == .unknown
            {
                print("INIT")
                let startTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
                self.writer.startWriting()
                self.writer.startSession(atSourceTime: startTime)

            }
            if self.writer.status == .failed
            {
                print("writer failed : \(self.writer.error!)")
            }
            if isVideo
            {
                if self.writerVideoInput.isReadyForMoreMediaData
                {
                    if self.writerVideoInput.append(sampleBuffer)
                    {
                        print("writing video")
                    }
                    else
                    {
                        print("failed to append video")
                    }
                }
                else
                {
                    print("video input data isn't ready ")
                }
            }
            else
            {
                if self.writerAudioInput.isReadyForMoreMediaData
                {
                    if self.writerAudioInput.append(sampleBuffer)
                    {
                        print("writing audio")
                    }
                    else
                    {
                        print("failed to append audio")
                    }

                }
                else
                {
                    print("audio input isn't ready")
                }
            }
        }
        else
        {
            print("sample buffer isnt ready ")
        }
}

问题是当我开始录制(将 isCapturing 标志设置为 true)时,前几帧被丢弃(原因是 FrameWasLate),Apple 的文档说这是因为 sampleBuffer 没有足够快地释放!,但我所做的只是初始化Avassetwriter而已!.

我试图将编码函数放在串行队列中,但没有成功!怎么了 ?!

4

0 回答 0