1

I'm trying to save captured video & audio through a camera and mic on an iOS device using AVAssetWriter. I finished developing a basic function like a user can record both image(video) and audio using CMSampleBuffer, and now I'm trying to make the mute function available. I guess while isMuted is true, I need to append empty (or silent?) CMSampleBuffer to assetWriterAudioInput, but is that right approach? If not could you point me in the right direction? Also, in createSilenceBuffer function, how can I create empty/slient CMSampleBuffer?

var isMuted: Bool = false
var assetWriterAudioInput: AVAssetWriterInput?

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    if output == videoOutput {
        guard assetWriterVideoInput?.isReadyForMoreMediaData == true else { return }
        assetWriterVideoInput?.append(sampleBuffer)
    } else if output == audioOutput {
        if issued {
            let silenceBuffer = createSilenceBuffer(from sampleBuffer)
            assetWriterAudioInput?.append(silenceBuffer)
        } else {
            assetWriterAudioInput?.append(sampleBuffer)
        }
    }
}

func createSilenceBuffer(from sampleBuffer: CMSampleBuffer) -> CMSampleBuffer {
    // return new CMSampleBuffer without audio??
}
Yuuu
  • 715
  • 1
  • 9
  • 32

1 Answers1

0

You can use this func to mute incoming audio CMSampleBuffers

private func _cacheAudioBuffer(sampleBuffer: CMSampleBuffer, isMuted: Bool) {
   if isMuted,
   let ref = CMSampleBufferGetDataBuffer(sampleBuffer) {
    CMBlockBufferFillDataBytes(
        with: 0,
        blockBuffer: ref,
        offsetIntoDestination: 0,
        dataLength: CMBlockBufferGetDataLength(ref))
   }
       // Continue here with muted samplebuffer
}
YYfim
  • 1,402
  • 1
  • 9
  • 24