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 { diff --git a/src/PlayerScripts.js b/src/PlayerScripts.js index 377d6d2..7e7aa2b 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;`; }, diff --git a/src/YoutubeIframe.js b/src/YoutubeIframe.js index ca95eae..774a8c1 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,17 @@ 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()); + }, }), [], );