From 3a32d67087c39bcf7904043d15a2fdba65307f4e Mon Sep 17 00:00:00 2001 From: Olivier Bouillet <62574056+freeboub@users.noreply.github.com> Date: Mon, 2 Sep 2024 15:40:38 +0200 Subject: [PATCH] fix(ios): ensure behavior is correct with empty text track list (#4123) * fix(ios): ensure behavior is correct with empty text track list --- examples/basic/src/constants/general.ts | 5 +++++ ios/Video/RCTVideo.swift | 23 +++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/examples/basic/src/constants/general.ts b/examples/basic/src/constants/general.ts index 665a23cd..e0d12185 100644 --- a/examples/basic/src/constants/general.ts +++ b/examples/basic/src/constants/general.ts @@ -78,6 +78,11 @@ export const srcAllPlatformList = [ uri: 'https://bitmovin-a.akamaihd.net/content/sintel/hls/playlist.m3u8', startPosition: 50000, }, + { + description: 'mp3 with texttrack', + uri: 'https://traffic.libsyn.com/democracynow/wx2024-0702_SOT_DeadCalm-LucileSmith-FULL-V2.mxf-audio.mp3', // an mp3 file + textTracks: [], // empty text track list + }, { description: 'BigBugBunny sideLoaded subtitles', // sideloaded subtitles wont work for streaming like HLS on ios diff --git a/ios/Video/RCTVideo.swift b/ios/Video/RCTVideo.swift index f1ca220f..e735d5d4 100644 --- a/ios/Video/RCTVideo.swift +++ b/ios/Video/RCTVideo.swift @@ -42,7 +42,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH private var _repeat = false private var _isPlaying = false private var _allowsExternalPlayback = true - private var _textTracks: [TextTrack]? + private var _textTracks: [TextTrack] = [] private var _selectedTextTrackCriteria: SelectedTrackCriteria? private var _selectedAudioTrackCriteria: SelectedTrackCriteria? private var _playbackStalled = false @@ -598,7 +598,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH } func playerItemPrepareText(asset: AVAsset!, assetOptions: NSDictionary?, uri: String) async -> AVPlayerItem { - if (self._textTracks == nil) || self._textTracks?.isEmpty == true || (uri.hasSuffix(".m3u8")) { + if self._textTracks.isEmpty == true || (uri.hasSuffix(".m3u8")) { return await self.playerItemPropegateMetadata(AVPlayerItem(asset: asset)) } @@ -612,7 +612,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH textTracks: self._textTracks ) - if validTextTracks.count != self._textTracks?.count { + if validTextTracks.count != self._textTracks.count { self.setTextTracks(validTextTracks) } @@ -963,8 +963,8 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH func setSelectedTextTrack(_ selectedTextTrack: SelectedTrackCriteria?) { _selectedTextTrackCriteria = selectedTextTrack - if _textTracks != nil { // sideloaded text tracks - RCTPlayerOperations.setSideloadedText(player: _player, textTracks: _textTracks!, criteria: _selectedTextTrackCriteria) + if !_textTracks.isEmpty { // sideloaded text tracks + RCTPlayerOperations.setSideloadedText(player: _player, textTracks: _textTracks, criteria: _selectedTextTrackCriteria) } else { // text tracks included in the HLS playlist§ Task { await RCTPlayerOperations.setMediaSelectionTrackForCharacteristic(player: _player, characteristic: AVMediaCharacteristic.legible, @@ -979,8 +979,11 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH } func setTextTracks(_ textTracks: [TextTrack]?) { - _textTracks = textTracks - + if textTracks == nil { + _textTracks = [] + } else { + _textTracks = textTracks! + } // in case textTracks was set after selectedTextTrack if _selectedTextTrackCriteria != nil { setSelectedTextTrack(_selectedTextTrackCriteria) } } @@ -1294,7 +1297,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH _playerItem = nil _source = nil _chapters = nil - _textTracks = nil + _textTracks = [] _selectedTextTrackCriteria = nil _selectedAudioTrackCriteria = nil _presentingViewController = nil @@ -1448,7 +1451,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH "orientation": orientation, ], "audioTracks": audioTracks, - "textTracks": self._textTracks?.compactMap { $0.json } ?? textTracks.map(\.json), + "textTracks": self._textTracks.compactMap { $0.json } ?? textTracks.map(\.json), "target": self.reactTag as Any]) } @@ -1646,7 +1649,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH if onTextTracks != nil { Task { let textTracks = await RCTVideoUtils.getTextTrackInfo(self._player) - self.onTextTracks?(["textTracks": self._textTracks?.compactMap { $0.json } ?? textTracks.compactMap(\.json)]) + self.onTextTracks?(["textTracks": self._textTracks.compactMap { $0.json } ?? textTracks.compactMap(\.json)]) } }