【问题标题】:Mute Audio AVAssetWriterInput while recording录音时静音音频 AVAssetWriterInput
【发布时间】:2012-08-01 12:20:12
【问题描述】:

我正在使用AVAssetWriter 录制视频和音频,分别从AVCaptureVideoDataOutputAVCaptureAudioDataOutput 附加CMSampleBuffer。我想做的是由用户自行决定在录制期间将音频静音。

我假设最好的方法是创建一个空的CMSampleBuffer

CMSampleBufferRef sb;
CMSampleBufferCreate(kCFAllocatorDefault, NULL, YES, NULL, NULL, NULL, 0, 1, &sti, 0, NULL, &sb);
[_audioInputWriter appendSampleBuffer:sb];
CFRelease(sb);

但这不起作用,所以我假设我需要创建一个静音音频缓冲区。我该怎么做?有没有更好的方法?

【问题讨论】:

    标签: ios ios5 avfoundation


    【解决方案1】:

    我之前通过调用处理 SampleBuffer 中的数据并将其全部归零的函数来完成此操作。如果您的音频格式未使用 SInt16 样本大小,则可能需要对此进行修改。

    您还可以使用相同的技术以其他方式处理音频。

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 
    {
        if(isMute){
            [self muteAudioInBuffer:sampleBuffer];
        }
    }
    
    
    - (void) muteAudioInBuffer:(CMSampleBufferRef)sampleBuffer 
    {
    
        CMItemCount numSamples = CMSampleBufferGetNumSamples(sampleBuffer);
        NSUInteger channelIndex = 0;
    
        CMBlockBufferRef audioBlockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
        size_t audioBlockBufferOffset = (channelIndex * numSamples * sizeof(SInt16));
        size_t lengthAtOffset = 0;
        size_t totalLength = 0;
        SInt16 *samples = NULL;
        CMBlockBufferGetDataPointer(audioBlockBuffer, audioBlockBufferOffset, &lengthAtOffset, &totalLength, (char **)(&samples));
    
        for (NSInteger i=0; i<numSamples; i++) {
                samples[i] = (SInt16)0;
        }
    
    }
    

    【讨论】:

    • 附带说明,您可以乘以 0 到 n 之间的数字来减少/增加音量百分比。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多