Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Screen recording audio and video out of sync

I use startCaptureWithHandler to record screen and AVAssetWriter appendSampleBuffer: to save audio and video ,but when played the saved file audio and video are out of sync. I don t know if it s a AVAssetWriterInputr setup problem,here is my code

  NSDictionary *audioCompressionSettings = @{
                                                AVEncoderBitRatePerChannelKey : @(64000),
                                                AVFormatIDKey : @(kAudioFormatMPEG4AAC),
                                                AVNumberOfChannelsKey : @(2),
                                                AVSampleRateKey : @(44100) };

  AVAssetWriterInput  *audioAssetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioCompressionSettings];

    audioAssetWriterInput.expectsMediaDataInRealTime = YES;
    [_assetWriter addInput:audioAssetWriterInput];

    NSDictionary *videoCompressSetting = @{AVVideoAverageBitRateKey:@(screenWidth*screenHeight*5),
                                           AVVideoMaxKeyFrameIntervalKey:@(30),
                                           AVVideoProfileLevelKey : AVVideoProfileLevelH264MainAutoLevel};
    
    
    NSDictionary *codecSetting = @{AVVideoCodecKey:AVVideoCodecTypeH264,
                                   AVVideoScalingModeKey : AVVideoScalingModeResize,
                                   AVVideoWidthKey:@(screenWidth*2),
                                   AVVideoHeightKey:@(screenHeight*2),
                                   AVVideoCompressionPropertiesKey:videoCompressSetting
    };
    
 AVAssetWriterInput* videoAssetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:codecSetting];
    videoAssetWriterInput.expectsMediaDataInRealTime = YES;
    [_assetWriter addInput:videoAssetWriterInput];


Screen recording audio and video out of sync
 
 
Q