feat(ios): Add ios support for accessing WebVTT Subtitle Content (#3541)
* feature: add support to get subtitle content data * refactor: return a string of the subtitles Push the parsing/formatting to the consumer side. * chore: add types for new subtitle feature * chore: run swiftlint and swiftformat * chore: add documentation for new onSubtitleTracks callback * chore: added test uri; basic implementation of feature; hotfix onTextTracks added optional chaining for `return x?.selected` because tracks that don't have a track selected either by default or manually will return undefined and this can cause an error. * feat: rename onSubtitleTracks to onTextTrackDataChanged Renamed the onSubtitleTracks event to onTextTrackDataChanged across the codebase to clearly indicate the callback's purpose: being called when the text track's data changes. This change is reflected in the events documentation, example usage in VideoPlayer.tsx, and the relevant iOS implementation files for consistency and clarity, in line with PR feedback. * chore: omit target property target could be confusing for users so we have removed it. using the delete operator instead of using {target,...eventData} as that would give an eslint error about unused vars.
This commit is contained in:
@@ -26,11 +26,12 @@ protocol RCTPlayerObserverHandler: RCTPlayerObserverHandlerObjc {
|
||||
func handleExternalPlaybackActiveChange(player: AVPlayer, change: NSKeyValueObservedChange<Bool>)
|
||||
func handleViewControllerOverlayViewFrameChange(overlayView: UIView, change: NSKeyValueObservedChange<CGRect>)
|
||||
func handleTracksChange(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<[AVPlayerItemTrack]>)
|
||||
func handleLegibleOutput(strings: [NSAttributedString])
|
||||
}
|
||||
|
||||
// MARK: - RCTPlayerObserver
|
||||
|
||||
class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate {
|
||||
class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate, AVPlayerItemLegibleOutputPushDelegate {
|
||||
weak var _handlers: RCTPlayerObserverHandler?
|
||||
|
||||
var player: AVPlayer? {
|
||||
@@ -57,8 +58,11 @@ class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate {
|
||||
|
||||
// handle timedMetadata
|
||||
let metadataOutput = AVPlayerItemMetadataOutput()
|
||||
let legibleOutput = AVPlayerItemLegibleOutput()
|
||||
playerItem.add(metadataOutput)
|
||||
playerItem.add(legibleOutput)
|
||||
metadataOutput.setDelegate(self, queue: .main)
|
||||
legibleOutput.setDelegate(self, queue: .main)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,6 +117,14 @@ class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
func legibleOutput(_: AVPlayerItemLegibleOutput,
|
||||
didOutputAttributedStrings strings: [NSAttributedString],
|
||||
nativeSampleBuffers _: [Any],
|
||||
forItemTime _: CMTime) {
|
||||
guard let _handlers else { return }
|
||||
_handlers.handleLegibleOutput(strings: strings)
|
||||
}
|
||||
|
||||
func addPlayerObservers() {
|
||||
guard let player, let _handlers else {
|
||||
return
|
||||
|
Reference in New Issue
Block a user