From b73baad2c2c0c6ea701d865eee32d4e94ae58178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Kueny?= Date: Mon, 4 Mar 2024 10:43:33 +0100 Subject: [PATCH] fix(ios): add text tracks only if we successfully insertTimeRage (#3557) insertTimeRage can fail & if we add failed textTrack to our validTextTracks array, video can crash later on selectTextTrack we also add en empty textTrack only we we have validTextTrack related to https://github.com/react-native-video/react-native-video/issues/3480 --- ios/Video/Features/RCTVideoUtils.swift | 27 +++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/ios/Video/Features/RCTVideoUtils.swift b/ios/Video/Features/RCTVideoUtils.swift index 3a1011e6..9fd6bec8 100644 --- a/ios/Video/Features/RCTVideoUtils.swift +++ b/ios/Video/Features/RCTVideoUtils.swift @@ -299,23 +299,32 @@ enum RCTVideoUtils { if let textTracks { for i in 0 ..< tracks.count { guard let track = tracks[i]?.first else { continue } // fix when there's no textTrackAsset - validTextTracks.append(textTracks[i]) let textCompTrack: AVMutableCompositionTrack! = mixComposition.addMutableTrack(withMediaType: AVMediaType.text, preferredTrackID: kCMPersistentTrackID_Invalid) - try? textCompTrack.insertTimeRange( - CMTimeRangeMake(start: .zero, duration: videoAsset.timeRange.duration), - of: track, - at: .zero - ) + + do { + try textCompTrack.insertTimeRange( + CMTimeRangeMake(start: .zero, duration: videoAsset.timeRange.duration), + of: track, + at: .zero + ) + validTextTracks.append(textTracks[i]) + } catch { + // TODO: upgrade error by call some props callback to better inform user + print("Error occurred on textTrack insert attempt: \(error.localizedDescription)") + continue + } } } return }.then { - let emptyVttFile: TextTrack? = self.createEmptyVttFile() - if emptyVttFile != nil { - validTextTracks.append(emptyVttFile!) + if !validTextTracks.isEmpty { + let emptyVttFile: TextTrack? = self.createEmptyVttFile() + if emptyVttFile != nil { + validTextTracks.append(emptyVttFile!) + } } fulfill(validTextTracks)