Fix/allow text track selection by index (#4124)

* fix(ios): ensure behavior is correct with empty text track list
* fix(ios): add index to text tracks reported
This commit is contained in:
Olivier Bouillet 2024-09-02 17:01:39 +02:00 committed by GitHub
parent 2fa6c43615
commit fbe570d62f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 256 additions and 242 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1394,6 +1394,20 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
} }
} }
func extractJsonWithIndex(from tracks: [TextTrack]) -> [NSDictionary]? {
if tracks.isEmpty {
// No tracks, need to return nil to handle
return nil
}
// Map each enumerated pair to include the index in the json dictionary
let mappedTracks = tracks.enumerated().compactMap { index, track -> NSDictionary? in
guard let json = track.json?.mutableCopy() as? NSMutableDictionary else { return nil }
json["index"] = index // Insert the index into the json dictionary
return json
}
return mappedTracks
}
func handleReadyToPlay() { func handleReadyToPlay() {
guard let _playerItem else { return } guard let _playerItem else { return }
@ -1452,7 +1466,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
"orientation": orientation, "orientation": orientation,
], ],
"audioTracks": audioTracks, "audioTracks": audioTracks,
"textTracks": self._textTracks.compactMap { $0.json } ?? textTracks.map(\.json), "textTracks": extractJsonWithIndex(from: _textTracks) ?? textTracks.map(\.json),
"target": self.reactTag as Any]) "target": self.reactTag as Any])
} }
@ -1650,7 +1664,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
if onTextTracks != nil { if onTextTracks != nil {
Task { Task {
let textTracks = await RCTVideoUtils.getTextTrackInfo(self._player) let textTracks = await RCTVideoUtils.getTextTrackInfo(self._player)
self.onTextTracks?(["textTracks": self._textTracks.compactMap { $0.json } ?? textTracks.compactMap(\.json)]) self.onTextTracks?(["textTracks": extractJsonWithIndex(from: _textTracks) ?? textTracks.compactMap(\.json)])
} }
} }