From 7b8f79b36a6eda1452b3cab1f35165f2ebcf9769 Mon Sep 17 00:00:00 2001 From: Ash Mishra Date: Mon, 27 Aug 2018 10:42:49 -0700 Subject: [PATCH] added an onCaptionsDeviceSettings event --- Video.js | 8 ++++++++ ios/RCTVideo.h | 1 + ios/RCTVideo.m | 15 ++++++++++++--- ios/RCTVideoManager.m | 1 + 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Video.js b/Video.js index bf64e9d5..58f57922 100644 --- a/Video.js +++ b/Video.js @@ -151,6 +151,12 @@ export default class Video extends Component { } }; + _onCaptionsDeviceSetting = (event) => { + if (this.props.onCaptionsDeviceSetting) { + this.props.onCaptionsDeviceSetting(event.nativeEvent); + } + }; + _onPlaybackStalled = (event) => { if (this.props.onPlaybackStalled) { this.props.onPlaybackStalled(event.nativeEvent); @@ -241,6 +247,7 @@ export default class Video extends Component { onVideoFullscreenPlayerWillDismiss: this._onFullscreenPlayerWillDismiss, onVideoFullscreenPlayerDidDismiss: this._onFullscreenPlayerDidDismiss, onReadyForDisplay: this._onReadyForDisplay, + onCaptionsDeviceSetting: this._onCaptionsDeviceSetting, onPlaybackStalled: this._onPlaybackStalled, onPlaybackResume: this._onPlaybackResume, onPlaybackRateChange: this._onPlaybackRateChange, @@ -379,6 +386,7 @@ Video.propTypes = { onFullscreenPlayerWillDismiss: PropTypes.func, onFullscreenPlayerDidDismiss: PropTypes.func, onReadyForDisplay: PropTypes.func, + onCaptionsDeviceSetting: PropTypes.func, onPlaybackStalled: PropTypes.func, onPlaybackResume: PropTypes.func, onPlaybackRateChange: PropTypes.func, diff --git a/ios/RCTVideo.h b/ios/RCTVideo.h index 1c471236..a556b3c3 100644 --- a/ios/RCTVideo.h +++ b/ios/RCTVideo.h @@ -23,6 +23,7 @@ @property (nonatomic, copy) RCTBubblingEventBlock onVideoFullscreenPlayerWillDismiss; @property (nonatomic, copy) RCTBubblingEventBlock onVideoFullscreenPlayerDidDismiss; @property (nonatomic, copy) RCTBubblingEventBlock onReadyForDisplay; +@property (nonatomic, copy) RCTBubblingEventBlock onCaptionsDeviceSetting; @property (nonatomic, copy) RCTBubblingEventBlock onPlaybackStalled; @property (nonatomic, copy) RCTBubblingEventBlock onPlaybackResume; @property (nonatomic, copy) RCTBubblingEventBlock onPlaybackRateChange; diff --git a/ios/RCTVideo.m b/ios/RCTVideo.m index 8e810b6b..f5e7f4e8 100644 --- a/ios/RCTVideo.m +++ b/ios/RCTVideo.m @@ -59,6 +59,7 @@ static int const RCTVideoUnset = -1; NSDictionary* _fullscreenOptions; BOOL _fullscreenPlayerPresented; UIViewController * _presentingViewController; + BOOL _deviceCaptionsEnabled; } - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher @@ -316,6 +317,10 @@ static int const RCTVideoUnset = -1; [self removePlayerTimeObserver]; [self removePlayerItemObservers]; + CFArrayRef captioningMediaCharacteristics = MACaptionAppearanceCopyPreferredCaptioningMediaCharacteristics(kMACaptionAppearanceDomainUser); + NSArray *captionSettings = (__bridge NSArray*)captioningMediaCharacteristics; + _deviceCaptionsEnabled = [captionSettings containsObject:AVMediaCharacteristicTranscribesSpokenDialogForAccessibility]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // perform on next run loop, otherwise other passed react-props may not be set @@ -353,6 +358,12 @@ static int const RCTVideoUnset = -1; }); } + if (self.onCaptionsDeviceSetting) { + self.onCaptionsDeviceSetting(@{@"deviceCaptionsEnabled": [NSNumber numberWithBool:_deviceCaptionsEnabled], + @"target": self.reactTag + }); + } + }); _videoLoadStarted = YES; } @@ -875,9 +886,7 @@ static int const RCTVideoUnset = -1; // in the situation that a selected text track is not available (eg. specifies a textTrack not available) if (![type isEqualToString:@"disabled"] && selectedTrackIndex == RCTVideoUnset) { - CFArrayRef captioningMediaCharacteristics = MACaptionAppearanceCopyPreferredCaptioningMediaCharacteristics(kMACaptionAppearanceDomainUser); - NSArray *captionSettings = (__bridge NSArray*)captioningMediaCharacteristics; - if ([captionSettings containsObject:AVMediaCharacteristicTranscribesSpokenDialogForAccessibility]) { + if (_deviceCaptionsEnabled) { selectedTrackIndex = 0; // If we can't find a match, use the first available track NSString *systemLanguage = [[NSLocale preferredLanguages] firstObject]; for (int i = 0; i < textTracks.count; ++i) { diff --git a/ios/RCTVideoManager.m b/ios/RCTVideoManager.m index 9d902392..683238a1 100644 --- a/ios/RCTVideoManager.m +++ b/ios/RCTVideoManager.m @@ -54,6 +54,7 @@ RCT_EXPORT_VIEW_PROPERTY(onVideoFullscreenPlayerDidPresent, RCTBubblingEventBloc RCT_EXPORT_VIEW_PROPERTY(onVideoFullscreenPlayerWillDismiss, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onVideoFullscreenPlayerDidDismiss, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onReadyForDisplay, RCTBubblingEventBlock); +RCT_EXPORT_VIEW_PROPERTY(onCaptionsDeviceSetting, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onPlaybackStalled, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onPlaybackResume, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onPlaybackRateChange, RCTBubblingEventBlock);