Skip to content

Commit a155335

Browse files
committed
add playlist support
1 parent 4a9d276 commit a155335

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

doc/readme.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
- width
3232
- ref
3333
- videoId
34+
- playList,
35+
- playListStartIndex,
3436
- play
3537
- onChangeState
3638
- onReady
@@ -41,6 +43,7 @@
4143
- playbackRate
4244
- onPlaybackRateChange
4345
- initialPlayerParams
46+
- webViewStyle
4447

4548
### Ref functions
4649

@@ -88,10 +91,29 @@ const App = () => {
8891

8992
## videoId
9093

91-
**REQUIRED, _String_**
94+
**_String_**
9295

9396
Specifies the YouTube Video ID of the video to be played.
9497

98+
## playList
99+
100+
**_Array<String> | String_**
101+
102+
Specifies the playlist to play. It can be either the playlist ID or a list of video IDs
103+
104+
`playList={'PLbpi6ZahtOH6Blw3RGYpWkSByi_T7Rygb'}`
105+
106+
or
107+
108+
`playList={['QRt7LjqJ45k', 'fHsa9DqmId8']}`
109+
110+
## playListStartIndex
111+
112+
**_Number_**
113+
114+
Starts the playlist from the given index
115+
**Works only if the playlist is a list of video IDs.**
116+
95117
## play
96118

97119
**_Boolean_**
@@ -200,3 +222,7 @@ The `playbackRate` that the API passes to the event listener function will be a
200222
| iv_load_policy | Number | | https://developers.google.com/youtube/player_parameters#iv_load_policy |
201223
| modestbranding | boolean | false | https://developers.google.com/youtube/player_parameters#modestbranding |
202224
| rel | boolean | false | https://developers.google.com/youtube/player_parameters#rel |
225+
226+
## webViewStyle
227+
228+
A style prop that will be given to the webview

src/PlayerScripts.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ player.seekTo(${seconds}, ${allowSeekAhead})
3333
setPlaybackRate: playbackRate =>
3434
`player.setPlaybackRate(${playbackRate}); true;`,
3535
setVolume: volume => `player.setVolume(${volume}); true;`,
36+
loadPlaylist: (playList, startIndex) => `
37+
player.loadPlaylist({playlist: ${JSON.stringify(playList)},
38+
index: ${startIndex || 0}}); true;`,
3639
};
3740

3841
export const MAIN_SCRIPT = (
3942
videoId,
43+
playList,
4044
{
4145
loop = false,
4246
controls = true,
@@ -89,7 +93,7 @@ export const MAIN_SCRIPT = (
8993
player = new YT.Player('player', {
9094
height: '1000',
9195
width: '1000',
92-
videoId: '${videoId}',
96+
videoId: '${videoId || ''}',
9397
playerVars: {
9498
playsinline: 1,
9599
loop: ${loop ? 1 : 0},
@@ -104,6 +108,8 @@ export const MAIN_SCRIPT = (
104108
modestbranding: ${modestbranding ? 1 : 0},
105109
rel: ${rel ? 1 : 0},
106110
start: ${start},
111+
listType: '${typeof playList === 'string' ? 'playlist' : ''}',
112+
list: '${typeof playList === 'string' ? playList : ''}',
107113
},
108114
events: {
109115
'onReady': onPlayerReady,

src/YoutubeIframe.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import React, {
66
forwardRef,
77
useState,
88
} from 'react';
9-
import {View} from 'react-native';
9+
import {View, StyleSheet} from 'react-native';
1010
import WebView from 'react-native-webview';
1111
import {PLAYER_STATES, PLAYER_ERROR} from './constants';
1212
import {EventEmitter} from 'events';
@@ -17,6 +17,8 @@ const YoutubeIframe = (
1717
height,
1818
width,
1919
videoId,
20+
playList,
21+
playListStartIndex,
2022
play = false,
2123
onChangeState,
2224
onReady,
@@ -27,6 +29,7 @@ const YoutubeIframe = (
2729
playbackRate = 1,
2830
onPlaybackRateChange,
2931
initialPlayerParams = {},
32+
webViewStyle,
3033
},
3134
ref,
3235
) => {
@@ -121,6 +124,11 @@ const YoutubeIframe = (
121124
case 'playerReady':
122125
onReady();
123126
setPlayerReady(true);
127+
if (Array.isArray(playList)) {
128+
webViewRef.current.injectJavaScript(
129+
PLAYER_FUNCTIONS.loadPlaylist(playList, playListStartIndex),
130+
);
131+
}
124132
break;
125133
case 'playerQualityChange':
126134
onPlaybackQualityChange(message.data);
@@ -145,20 +153,27 @@ const YoutubeIframe = (
145153
onPlaybackQualityChange,
146154
onError,
147155
onPlaybackRateChange,
156+
playListStartIndex,
157+
playList,
148158
],
149159
);
150160

151161
return (
152162
<View style={{height, width}}>
153163
<WebView
164+
style={[webViewStyle, styles.webView]}
154165
ref={webViewRef}
155166
originWhitelist={['*']}
156-
source={{html: MAIN_SCRIPT(videoId, initialPlayerParams)}}
167+
source={{html: MAIN_SCRIPT(videoId, playList, initialPlayerParams)}}
157168
allowsInlineMediaPlayback
158169
onMessage={onWebMessage}
159170
/>
160171
</View>
161172
);
162173
};
163174

175+
const styles = StyleSheet.create({
176+
webView: {backgroundColor: 'transparent'},
177+
});
178+
164179
export default forwardRef(YoutubeIframe);

0 commit comments

Comments
 (0)