fix: pre-install audioMix in playIndex to prevent video freeze on record start

Setting playerItem.audioMix while AVPlayer is actively playing causes
the audio/video pipeline to reconfigure, stalling video playback.

Fix: install the MTAudioProcessingTap audioMix at item creation time
(in playIndex, before replaceCurrentItem), so the pipeline is already
configured when playback begins. startRecording now only flips the
isRecording flag and skips re-installation if audioMix exists.

Also removed the guard that blocked tap installation when tracks
weren't available yet (HLS streams) — the wildcard trackID
(kCMPersistentTrackID_Invalid) handles this case.
This commit is contained in:
yumoqing 2026-06-28 10:30:20 +08:00
parent cff2bd2793
commit 9160b3ce2b
2 changed files with 13 additions and 7 deletions

View File

@ -186,6 +186,9 @@ final class PlayerBridge: ObservableObject {
let output = AVPlayerItemVideoOutput(pixelBufferAttributes: pixelBufferAttributes)
playerItem.add(output)
videoOutput = output
// Tap audioMix
screenRecorder.installAudioTap(on: playerItem)
#endif
player.replaceCurrentItem(with: playerItem)

View File

@ -319,7 +319,8 @@ final class PlayerRecorder: NSObject {
// MARK: - Audio Tap Setup
/// Tap使 playerItem.tracks HLS asset
private func installAudioTap(on playerItem: AVPlayerItem) {
/// playerItem playIndex audioMix
func installAudioTap(on playerItem: AVPlayerItem) {
//
if gTapContext == nil {
gTapContext = AudioTapContext()
@ -344,10 +345,8 @@ final class PlayerRecorder: NSObject {
}
}
guard audioTrackID != kCMPersistentTrackID_Invalid else {
print("[Recorder:DIAG] ⚠️ No enabled audio track in playerItem.tracks!")
writeDiagLog("No audio track in playerItem.tracks (count=\(itemTracks.count))")
return
if audioTrackID == kCMPersistentTrackID_Invalid {
print("[Recorder:DIAG] ⚠️ No enabled audio track found yet (tracks=\(itemTracks.count)), using wildcard")
}
print("[Recorder:DIAG] Using audio trackID=\(audioTrackID)")
@ -431,8 +430,12 @@ final class PlayerRecorder: NSObject {
isRunning = false
recordStartTime = startTime
// Tap asset track
installAudioTap(on: playerItem)
// Tap playIndex
if playerItem.audioMix == nil {
installAudioTap(on: playerItem)
} else {
print("[Recorder:DIAG] audioMix already installed, skipping")
}
let tempFilename = "temp_recording_\(Int(Date().timeIntervalSince1970)).mp4"
let url = saveDirectory.appendingPathComponent(tempFilename)