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

Add playback rate get/set option. #480

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
28 changes: 28 additions & 0 deletions RCTYouTubeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,32 @@ - (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];

[youtube getPlaybackRate:^(float response, NSError * _Nullable error) {
if (error) {
reject(@"Error getting playback rate of video from RCTYouTube", @"", error);
} else {
NSNumber *rate = [NSNumber numberWithInt:response];
resolve(rate);
}
}];
}];
}

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 @@ -106,6 +106,8 @@ The iOS implementation of this player uses the official YouTube iFrame under the
- `getCurrentTime()`: 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.
- `getDuration()`: 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)*: Returns the playback rate of the currently playing video.
* `setPlaybackRate(rate)` *(iOS)*: Sets the suggested playback rate for the current video.

### Standalone Player (iOS)

Expand Down
12 changes: 12 additions & 0 deletions YouTube.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,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
2 changes: 2 additions & 0 deletions main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ declare class YouTube extends React.Component<YouTubeProps> {
getVideosIndex(): Promise<number>;
getCurrentTime(): Promise<number>;
getDuration(): Promise<number>;
getPlaybackRate(): number;
setPlaybackRate(rate: number): void;
reloadIframe(): void;
}

Expand Down