Skip to content

Commit 1bd53a6

Browse files
authored
Merge pull request #65 from draku29/refactor/player-effect
Refactor/player effect
2 parents db477ea + 47d143a commit 1bd53a6

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/PlayerScripts.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {MUTE_MODE, PAUSE_MODE, PLAY_MODE, UNMUTE_MODE} from './constants';
2+
13
export const PLAYER_FUNCTIONS = {
24
durationScript: `
35
window.ReactNativeWebView.postMessage(JSON.stringify({eventType: 'getDuration', data: player.getDuration()}));
@@ -40,6 +42,16 @@ player.seekTo(${seconds}, ${allowSeekAhead})
4042
index: ${startIndex || 0}}); true;`,
4143
};
4244

45+
export const playMode = {
46+
[PLAY_MODE]: PLAYER_FUNCTIONS.playVideo,
47+
[PAUSE_MODE]: PLAYER_FUNCTIONS.pauseVideo
48+
};
49+
50+
export const soundMode = {
51+
[MUTE_MODE]: PLAYER_FUNCTIONS.muteVideo,
52+
[UNMUTE_MODE]: PLAYER_FUNCTIONS.unMuteVideo,
53+
};
54+
4355
export const MAIN_SCRIPT = (
4456
videoId,
4557
playList,

src/YoutubeIframe.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {View, StyleSheet, Platform} from 'react-native';
1010
import WebView from 'react-native-webview';
1111
import {PLAYER_STATES, PLAYER_ERROR, CUSTOM_USER_AGENT} from './constants';
1212
import {EventEmitter} from 'events';
13-
import {MAIN_SCRIPT, PLAYER_FUNCTIONS} from './PlayerScripts';
13+
import {
14+
MAIN_SCRIPT,
15+
PLAYER_FUNCTIONS,
16+
playMode,
17+
soundMode,
18+
} from './PlayerScripts';
1419

1520
const YoutubeIframe = (
1621
{
@@ -94,23 +99,17 @@ const YoutubeIframe = (
9499
);
95100

96101
useEffect(() => {
97-
if (playerReady) {
98-
if (play) {
99-
webViewRef.current.injectJavaScript(PLAYER_FUNCTIONS.playVideo);
100-
} else {
101-
webViewRef.current.injectJavaScript(PLAYER_FUNCTIONS.pauseVideo);
102-
}
103-
104-
if (mute) {
105-
webViewRef.current.injectJavaScript(PLAYER_FUNCTIONS.muteVideo);
106-
} else {
107-
webViewRef.current.injectJavaScript(PLAYER_FUNCTIONS.unMuteVideo);
108-
}
109-
webViewRef.current.injectJavaScript(PLAYER_FUNCTIONS.setVolume(volume));
110-
webViewRef.current.injectJavaScript(
111-
PLAYER_FUNCTIONS.setPlaybackRate(playbackRate),
112-
);
102+
if (!playerReady) {
103+
return;
113104
}
105+
106+
[
107+
playMode[play],
108+
soundMode[mute],
109+
PLAYER_FUNCTIONS.setVolume(volume),
110+
PLAYER_FUNCTIONS.setPlaybackRate(playbackRate),
111+
].forEach(webViewRef.current.injectJavaScript);
112+
114113
}, [play, playerReady, mute, volume, playbackRate]);
115114

116115
const onWebMessage = useCallback(

src/constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2+
export const PLAY_MODE = true;
3+
export const PAUSE_MODE = false;
4+
export const MUTE_MODE = true;
5+
export const UNMUTE_MODE = false;
6+
17
export const PLAYER_STATES_NAMES = {
28
UNSTARTED: 'unstarted',
39
ENDED: 'ended',

0 commit comments

Comments
 (0)