feat: implement opacity to control visibility of subtitles (#3583)
* feat: implement opacity to control visibility of subtitles implemented per discussion on https://github.com/react-native-video/react-native-video/issues/3579 updated docs and linked onTextTrackDataChanged to the subtitle style to clarify intent on how to control visibility. * chore: update type so that we use a union of 0 1 vs any number * chore: run ktlint to get rid of white spaces * feat: add ability to have range of numbers for opacity; while, 0 will still not render the subtitles. added util function for safeGetFloat updated types * feat: add ability to suppress subtitles with opacity 0 add data structure for subtitle styles for extensibility * chore: run yarn check-ios * chore: update documentation to clarify differences between android and ios * Update android/src/main/java/com/brentvatne/exoplayer/ExoPlayerView.java Co-authored-by: Olivier Bouillet <62574056+freeboub@users.noreply.github.com> --------- Co-authored-by: Olivier Bouillet <62574056+freeboub@users.noreply.github.com>
This commit is contained in:
17
ios/Video/DataStructures/SubtitleStyle.swift
Normal file
17
ios/Video/DataStructures/SubtitleStyle.swift
Normal file
@@ -0,0 +1,17 @@
|
||||
struct SubtitleStyle {
|
||||
// Extend with more style properties as needed.
|
||||
private(set) var opacity: CGFloat
|
||||
|
||||
enum SubtitleStyleKeys {
|
||||
static let opacity = "opacity"
|
||||
}
|
||||
|
||||
init(opacity: CGFloat = 1) {
|
||||
self.opacity = opacity
|
||||
}
|
||||
|
||||
static func parse(from dictionary: [String: Any]?) -> SubtitleStyle {
|
||||
let opacity = dictionary?[SubtitleStyleKeys.opacity] as? CGFloat ?? 1
|
||||
return SubtitleStyle(opacity: opacity)
|
||||
}
|
||||
}
|
@@ -47,6 +47,8 @@ class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate, AVPla
|
||||
}
|
||||
}
|
||||
|
||||
var subtitleStyle: SubtitleStyle?
|
||||
|
||||
var playerItem: AVPlayerItem? {
|
||||
willSet {
|
||||
removePlayerItemObservers()
|
||||
@@ -63,6 +65,7 @@ class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate, AVPla
|
||||
playerItem.add(legibleOutput)
|
||||
metadataOutput.setDelegate(self, queue: .main)
|
||||
legibleOutput.setDelegate(self, queue: .main)
|
||||
legibleOutput.suppressesPlayerRendering = subtitleStyle?.opacity == 0 ? true : false
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -953,6 +953,12 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
|
||||
_playerObserver.playerLayer = nil
|
||||
}
|
||||
|
||||
@objc
|
||||
func setSubtitleStyle(_ style: [String: Any]) {
|
||||
let subtitleStyle = SubtitleStyle.parse(from: style)
|
||||
_playerObserver.subtitleStyle = subtitleStyle
|
||||
}
|
||||
|
||||
// MARK: - RCTVideoPlayerViewControllerDelegate
|
||||
|
||||
func videoPlayerViewControllerWillDismiss(playerViewController: AVPlayerViewController) {
|
||||
|
@@ -37,7 +37,7 @@ RCT_EXPORT_VIEW_PROPERTY(filterEnabled, BOOL);
|
||||
RCT_EXPORT_VIEW_PROPERTY(progressUpdateInterval, float);
|
||||
RCT_EXPORT_VIEW_PROPERTY(restoreUserInterfaceForPIPStopCompletionHandler, BOOL);
|
||||
RCT_EXPORT_VIEW_PROPERTY(localSourceEncryptionKeyScheme, NSString);
|
||||
|
||||
RCT_EXPORT_VIEW_PROPERTY(subtitleStyle, NSDictionary);
|
||||
/* Should support: onLoadStart, onLoad, and onError to stay consistent with Image */
|
||||
RCT_EXPORT_VIEW_PROPERTY(onVideoLoadStart, RCTDirectEventBlock);
|
||||
RCT_EXPORT_VIEW_PROPERTY(onVideoLoad, RCTDirectEventBlock);
|
||||
|
Reference in New Issue
Block a user