使用NextLevel iOS库录制音视频不同步问题求助
Hey there, let's walk through targeted fixes and checks for your audio-video sync problem with NextLevel. Since you can't reliably reproduce the issue, these are based on common edge cases with this library and AVFoundation that often cause desyncs:
Wait for Recording to Fully Stop Before Merging
Sometimes callingmergeClipsimmediately after stopping recording can catch AVFoundation mid-write of the final audio/video frames, leading to sync mismatches. Instead, hook into NextLevel'ssessionDidStopRecordingdelegate callback and add a short delay (0.2–0.5 seconds) before triggering your export. This gives the system time to finalize all clip data:func nextLevelSessionDidStopRecording(_ session: NextLevelSession) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in self?.exportVideo() } }Swap the Export Preset to Avoid Unnecessary Transcoding
AVAssetExportPresetMediumQualityforces transcoding, which can introduce sync issues if your original clips have mismatched frame rates or audio sample rates. Try usingAVAssetExportPresetPassthroughinstead—it skips re-encoding entirely, which reduces the chance of desync:session.mergeClips(usingPreset: AVAssetExportPresetPassthrough) { [weak self] (url, error) in // Your existing export completion logic here }If you do need quality reduction, use a preset that matches your recording resolution/frame rate (e.g.,
AVAssetExportPreset1280x720if that's your capture setting).Tighten Up Your Audio Session Configuration
iOS 11 has subtle audio session quirks that can throw off sync, especially if your app switches between playback and recording. Make sure you set the correct category before starting recording, and clean up afterward:// Call this before starting recording do { try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .videoRecording, options: .defaultToSpeaker) try AVAudioSession.sharedInstance().setActive(true) } catch { print("Audio session setup error: \(error.localizedDescription)") } // Call this after export completes do { try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation) } catch { print("Audio session teardown error: \(error.localizedDescription)") }Handle Background Interruptions Gracefully
Even a brief background switch (like a push notification) can pause/resume recording in a way that desyncs audio and video. Add listeners for app state changes to pause recording when the app resigns active, and resume when it becomes active again:// Add these in your view controller's viewDidLoad NotificationCenter.default.addObserver(self, selector: #selector(pauseRecording), name: UIApplication.willResignActiveNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(resumeRecordingIfNeeded), name: UIApplication.didBecomeActiveNotification, object: nil) @objc private func pauseRecording() { if NextLevel.shared.session?.isRecording == true { NextLevel.shared.stopRecording() } } @objc private func resumeRecordingIfNeeded() { // Add logic here to resume recording only if the user was actively holding the record button }Add Debug Logging to Catch Patterns
Since you can't reproduce the issue, add logging to capture details about each recorded clip's audio and video tracks. This can help you spot mismatched durations or errors when users report the problem:if let clips = NextLevel.shared.session?.clips { for clip in clips { let asset = AVAsset(url: clip.url) asset.loadValuesAsynchronously(forKeys: ["duration", "tracks"]) { do { try asset.validateValue(forKey: "tracks") let audioTracks = asset.tracks(withMediaType: .audio) let videoTracks = asset.tracks(withMediaType: .video) print("Clip \(clip.url.lastPathComponent): Audio duration = \(audioTracks.first?.timeRange.duration.seconds ?? 0)s | Video duration = \(videoTracks.first?.timeRange.duration.seconds ?? 0)s") } catch { print("Failed to load clip tracks: \(error.localizedDescription)") } } } }Consider Updating NextLevel (Carefully)
While you noted the issue isn't version-specific, 0.8.4 is quite old (circa 2017). Later versions of the library include fixes for AVFoundation-related sync bugs. Test updating to the latest version compatible with iOS 11 and Swift 4.0 in a staging environment to see if the issue resolves.
内容的提问来源于stack exchange,提问作者notaninja

