ios - 静音音频 AVCaptureSession

标签 ios audio avfoundation

我正在尝试将 AVCaptureSession 的音频静音和取消静音。一旦我开始 session ,我可以启用和禁用音频连接,但是一旦我播放视频,所有音频部分都被推回到视频的前面,只剩下视频的末尾没有声音。这似乎是一个时间戳问题,但我不知道这是怎么回事。以防万一我尝试调整音频样本缓冲区的 PTS 以匹配之前的视频缓冲区。

暂停

if (self.muted) {
    [self.session beginConfiguration];
    self.audioConnection.enabled = NO;
    [self.session commitConfiguration];
} else {
    [self.session beginConfiguration];
    self.audioConnection.enabled = YES;
    [self.session commitConfiguration];
}

为了调整时间戳,我抓取了最后一个时间戳

if ( connection == self.videoConnection ) {

    // Get timestamp
    CMTime timestamp = CMSampleBufferGetPresentationTimeStamp( sampleBuffer );
    testPreviousTimeStamp = timestamp;

然后我调整示例缓冲区上的时间戳

CMSampleBufferSetOutputPresentationTimeStamp (sampleBuffer,testPreviousTimeStamp);

if (![self.assetWriterAudioIn appendSampleBuffer:sampleBuffer]) {
        [self showError:[self.assetWriter error]];
                    NSLog(@"Problem writing audio sample buffer");
}

您知道问题可能是什么以及如何解决吗?

最佳答案

而不是调整我没有运气的时间戳。我刚刚向数据缓冲区写入了零。它有效,这样我不需要禁用/启用任何连接。

        // Write audio data to file
        if (readyToRecordAudio && readyToRecordVideo) {
            if (self.muted) {
                CMBlockBufferRef buffRef = CMSampleBufferGetDataBuffer(sampleBuffer);
                char fillByte = 0;
                CMBlockBufferFillDataBytes(fillByte,buffRef,0,CMBlockBufferGetDataLength(buffRef));
            }
            [self writeSampleBuffer:sampleBuffer ofType:AVMediaTypeAudio];
        }

关于ios - 静音音频 AVCaptureSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24459178/

相关文章:

ios - 使用自动布局使 3 个 UIbuttons 居中对齐

ios - CFDictionary 更新到 PHPhotoLibrary kCGImagePropertyExifDictionary 不采取

javascript - new Audio() 未在 Internet Explorer 中实现

ios - 从父级重新加载 TableViewController

ios - Google Analytics 位代码发布

matlab - .aac 音频文件的内容是什么?

android - 无法识别使用 cordova-plugin-media 生成的 Ionic 2 音频文件

ios - AVPlayer 在更新进度 slider 时导致崩溃,断言失败 -[AVPlayerPeriodicCaller initWithPlayer :interval:queue:block:]

ios - 为什么 audioRecorderDidFinishRecording 没有被调用?能够成功录制和播放音频

ios - 同时录制和播放音频的问题