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

iOS: Add setPlaybackRate & getPlaybackRate #313

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions RCTYouTubeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,34 @@ - (dispatch_queue_t)methodQueue {
}];
}

RCT_EXPORT_METHOD(playbackRate:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RCTYouTube *youtube = (RCTYouTube*)viewRegistry[reactTag];
if ([youtube isKindOfClass:[RCTYouTube class]]) {
NSNumber *rate = [NSNumber numberWithInt:[youtube playbackRate]];
if (rate) {
resolve(rate);
} else {
NSError *error = nil;
reject(@"Error getting play back rate time of video from RCTYouTube", @"", error);
}
} else {
RCTLogError(@"Cannot playbackRate: %@ (tag #%@) is not RCTYouTube", youtube, reactTag);
}
}];
}

RCT_EXPORT_METHOD(setPlaybackRate:(nonnull NSNumber *)reactTag rate:(nonnull NSNumber *)rate)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RCTYouTube *youtube = (RCTYouTube*)viewRegistry[reactTag];
if ([youtube isKindOfClass:[RCTYouTube class]]) {
[youtube setPlaybackRate:(float)[rate floatValue]];
} else {
RCTLogError(@"Cannot setPlaybackRate: %@ (tag #%@) is not RCTYouTube", youtube, reactTag);
}
}];
}

@end
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ The iOS implementation of this player uses the official YouTube iFrame under the
* `currentTime()`: Returns a Promise that results with the `currentTime` of the played video (in seconds) or errors with an errorMessage string. Should be used as an alternative for Android to `onProgress` event on iOS.
* `duration()` *(Android)*: Returns a Promise that results with the `duration` of the played video (in seconds) or errors with an errorMessage string. Should be used as an alternative for Android to `onProgress` event on iOS.
* `reloadIframe()` *(iOS)*: Specific props (`fullscreen`, `modestbranding`, `showinfo`, `rel`, `controls`, `origin`) can only be set at mounting and initial loading of the underlying WebView that holds the YouTube iFrame (Those are `<iframe>` parameters). If you want to change one of them during the lifecycle of the component, you should know the usability cost of loading the WebView again, and use this method right after the component received the updated prop.
* `getPlaybackRate()` *(iOS)*: Retrieves the playback rate of the currently playing video.
* `setPlaybackRate(rate)` *(iOS)*: sets the suggested playback rate for the current video

### Standalone Player (iOS)
#### Setup
Expand Down
4 changes: 4 additions & 0 deletions YTPlayerView/YTPlayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,10 @@ - (UIWebView *)createNewWebView {
}
}

// Add UserAgent to enable setPlaybackRate
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

return webView;
}

Expand Down
12 changes: 12 additions & 0 deletions YouTube.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ export default class YouTube extends React.Component {
this.setState({ playerParams: parsePlayerParams(this.props) });
}

setPlaybackRate(rate) {
NativeModules.YouTubeManager.setPlaybackRate(ReactNative.findNodeHandle(this), rate);
}

getPlaybackRate() {
return new Promise((resolve, reject) =>
NativeModules.YouTubeManager.getPlaybackRate(ReactNative.findNodeHandle(this))
.then(rate => resolve(rate))
.catch(errorMessage => reject(errorMessage))
);
}

render() {
return (
<RCTYouTube
Expand Down