From 9290638d80bcace6ff96a6200ba7fa486d86749f Mon Sep 17 00:00:00 2001 From: Hyo Date: Sun, 1 May 2022 22:02:09 +0900 Subject: [PATCH 1/4] Add play, stop, pause APIs (#1) --- src/PlayerScripts.js | 14 ++++++++++++- src/YoutubeIframe.js | 48 +++++++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/PlayerScripts.js b/src/PlayerScripts.js index 377d6d2..82ad7d0 100644 --- a/src/PlayerScripts.js +++ b/src/PlayerScripts.js @@ -38,6 +38,18 @@ true; return `player.setVolume(${volume}); true;`; }, + playVideoScript: () => { + return `player.playVideo(); true;`; + }, + + pauseVideoScript: () => { + return `player.pauseVideo(); true;`; + }, + + stopVideoScript: () => { + return `player.stopVideo(); true;`; + }, + seekToScript: (seconds, allowSeekAhead) => { return `player.seekTo(${seconds}, ${allowSeekAhead}); true;`; }, @@ -205,7 +217,7 @@ export const MAIN_SCRIPT = ( color: ${color}, start: ${start}, hl: ${playerLang}, - controls: ${controls_s}, + controls: 0, fs: ${preventFullScreen_s}, cc_lang_pref: '${cc_lang_pref_s}', iv_load_policy: ${iv_load_policy}, diff --git a/src/YoutubeIframe.js b/src/YoutubeIframe.js index ca95eae..af2c38c 100644 --- a/src/YoutubeIframe.js +++ b/src/YoutubeIframe.js @@ -1,27 +1,28 @@ -import React, { - useRef, - useMemo, - useState, - useEffect, - forwardRef, - useCallback, - useImperativeHandle, -} from 'react'; -import {Platform, StyleSheet, View} from 'react-native'; -import {EventEmitter} from 'events'; -import {WebView} from './WebView'; import { + CUSTOM_USER_AGENT, + DEFAULT_BASE_URL, PLAYER_ERROR, PLAYER_STATES, - DEFAULT_BASE_URL, - CUSTOM_USER_AGENT, } from './constants'; import { - playMode, - soundMode, MAIN_SCRIPT, PLAYER_FUNCTIONS, + playMode, + soundMode, } from './PlayerScripts'; +import {Platform, StyleSheet, View} from 'react-native'; +import React, { + forwardRef, + useCallback, + useEffect, + useImperativeHandle, + useMemo, + useRef, + useState, +} from 'react'; + +import {EventEmitter} from 'events'; +import {WebView} from './WebView'; import {deepComparePlayList} from './utils'; const YoutubeIframe = (props, ref) => { @@ -113,6 +114,21 @@ const YoutubeIframe = (props, ref) => { PLAYER_FUNCTIONS.seekToScript(seconds, allowSeekAhead), ); }, + playVideo: () => { + webViewRef.current.injectJavaScript( + PLAYER_FUNCTIONS.playVideoScript(), + ); + }, + pauseVideo: () => { + webViewRef.current.injectJavaScript( + PLAYER_FUNCTIONS.pauseVideoScript(), + ); + }, + stopVideo: () => { + webViewRef.current.injectJavaScript( + PLAYER_FUNCTIONS.stopVideoScript(), + ); + }, }), [], ); From 422c928a0a4fde278c144950e4ace163c684340d Mon Sep 17 00:00:00 2001 From: hyochan Date: Sun, 1 May 2022 22:07:31 +0900 Subject: [PATCH 2/4] Fix linting --- src/PlayerScripts.js | 6 +++--- src/YoutubeIframe.js | 8 ++------ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/PlayerScripts.js b/src/PlayerScripts.js index 82ad7d0..10c799a 100644 --- a/src/PlayerScripts.js +++ b/src/PlayerScripts.js @@ -39,15 +39,15 @@ true; }, playVideoScript: () => { - return `player.playVideo(); true;`; + return 'player.playVideo(); true;'; }, pauseVideoScript: () => { - return `player.pauseVideo(); true;`; + return 'player.pauseVideo(); true;'; }, stopVideoScript: () => { - return `player.stopVideo(); true;`; + return 'player.stopVideo(); true;'; }, seekToScript: (seconds, allowSeekAhead) => { diff --git a/src/YoutubeIframe.js b/src/YoutubeIframe.js index af2c38c..774a8c1 100644 --- a/src/YoutubeIframe.js +++ b/src/YoutubeIframe.js @@ -115,9 +115,7 @@ const YoutubeIframe = (props, ref) => { ); }, playVideo: () => { - webViewRef.current.injectJavaScript( - PLAYER_FUNCTIONS.playVideoScript(), - ); + webViewRef.current.injectJavaScript(PLAYER_FUNCTIONS.playVideoScript()); }, pauseVideo: () => { webViewRef.current.injectJavaScript( @@ -125,9 +123,7 @@ const YoutubeIframe = (props, ref) => { ); }, stopVideo: () => { - webViewRef.current.injectJavaScript( - PLAYER_FUNCTIONS.stopVideoScript(), - ); + webViewRef.current.injectJavaScript(PLAYER_FUNCTIONS.stopVideoScript()); }, }), [], From 1d31cf1b61fe7e50a2df32247983b87435e0a8cc Mon Sep 17 00:00:00 2001 From: hyochan Date: Sun, 1 May 2022 22:08:06 +0900 Subject: [PATCH 3/4] Revert controls --- src/PlayerScripts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PlayerScripts.js b/src/PlayerScripts.js index 10c799a..7e7aa2b 100644 --- a/src/PlayerScripts.js +++ b/src/PlayerScripts.js @@ -217,7 +217,7 @@ export const MAIN_SCRIPT = ( color: ${color}, start: ${start}, hl: ${playerLang}, - controls: 0, + controls: ${controls_s}, fs: ${preventFullScreen_s}, cc_lang_pref: '${cc_lang_pref_s}', iv_load_policy: ${iv_load_policy}, From 614e8cbe506367ee8fab0bd5169cbde1257fcd12 Mon Sep 17 00:00:00 2001 From: hyochan Date: Sun, 1 May 2022 22:20:31 +0900 Subject: [PATCH 4/4] Fix tsc --- index.d.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.d.ts b/index.d.ts index 3cba487..0ed9508 100644 --- a/index.d.ts +++ b/index.d.ts @@ -27,6 +27,9 @@ export interface YoutubeIframeRef { getPlaybackRate: () => Promise; getAvailablePlaybackRates: () => Promise; seekTo: (seconds: number, allowSeekAhead: boolean) => void; + playVideo: () => void; + pauseVideo: () => void; + stopVideo: () => void; } export interface InitialPlayerParams {