Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sideload text track issues on iOS #1157

Merged
merged 1 commit into from
Jul 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions ios/RCTVideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
static NSString *const playbackRate = @"rate";
static NSString *const timedMetadata = @"timedMetadata";

static int const RCTVideoUnset = -1;

@implementation RCTVideo
{
AVPlayer *_player;
Expand Down Expand Up @@ -807,7 +809,7 @@ - (void)setSelectedTextTrack:(NSDictionary *)selectedTextTrack {

- (void) setSideloadedText {
NSString *type = _selectedTextTrack[@"type"];
NSArray* textTracks = [self getTextTrackInfo];
NSArray *textTracks = [self getTextTrackInfo];

// The first few tracks will be audio & video track
int firstTextIndex = 0;
Expand All @@ -817,7 +819,7 @@ - (void) setSideloadedText {
}
}

int selectedTrackIndex = -1;
int selectedTrackIndex = RCTVideoUnset;

if ([type isEqualToString:@"disabled"]) {
// Do nothing. We want to ensure option is nil
Expand Down Expand Up @@ -846,29 +848,27 @@ - (void) setSideloadedText {
selectedTrackIndex = index;
}
}
}

// user's selected language might not be available, or system defaults have captions enabled
if (selectedTrackIndex == -1 || [type isEqualToString:@"default"]) {
CFArrayRef captioningMediaCharacteristics = MACaptionAppearanceCopyPreferredCaptioningMediaCharacteristics(kMACaptionAppearanceDomainUser);
NSArray *captionSettings = (__bridge NSArray*)captioningMediaCharacteristics;
if ([captionSettings containsObject: AVMediaCharacteristicTranscribesSpokenDialogForAccessibility]) {
// iterate through the textTracks to find a matching option, or default to the first object.
selectedTrackIndex = 0;

NSString * systemLanguage = [[NSLocale preferredLanguages] firstObject];
for (int i = 0; i < textTracks.count; ++i) {
NSDictionary *currentTextTrack = [textTracks objectAtIndex:i];
if ([systemLanguage isEqualToString:currentTextTrack[@"language"]]) {
selectedTrackIndex = i;
break;
}
} else { // type "system"
CFArrayRef captioningMediaCharacteristics = MACaptionAppearanceCopyPreferredCaptioningMediaCharacteristics(kMACaptionAppearanceDomainUser);
NSArray *captionSettings = (__bridge NSArray*)captioningMediaCharacteristics;
if ([captionSettings containsObject:AVMediaCharacteristicTranscribesSpokenDialogForAccessibility]) {
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) {
NSDictionary *currentTextTrack = [textTracks objectAtIndex:i];
if ([systemLanguage isEqualToString:currentTextTrack[@"language"]]) {
selectedTrackIndex = i;
break;
}
}
}
}

for (int i = firstTextIndex; i < _player.currentItem.tracks.count; ++i) {
BOOL isEnabled = i == selectedTrackIndex + firstTextIndex;
BOOL isEnabled = NO;
if (selectedTrackIndex != RCTVideoUnset) {
isEnabled = i == selectedTrackIndex + firstTextIndex;
}
[_player.currentItem.tracks[i] setEnabled:isEnabled];
}
}
Expand Down