Skip to content

Commit 9be16d7

Browse files
authored
Merge pull request #12 from LonelyCpp/bug-fixes
Bug fixes
2 parents d13844b + 7d83cff commit 9be16d7

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

src/YoutubeIframe.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import React, {
66
forwardRef,
77
useState,
88
} from 'react';
9-
import {View, StyleSheet} from 'react-native';
9+
import {View, StyleSheet, Platform} from 'react-native';
1010
import WebView from 'react-native-webview';
11-
import {PLAYER_STATES, PLAYER_ERROR} from './constants';
11+
import {PLAYER_STATES, PLAYER_ERROR, CUSTOM_USER_AGENT} from './constants';
1212
import {EventEmitter} from 'events';
1313
import {MAIN_SCRIPT, PLAYER_FUNCTIONS} from './PlayerScripts';
1414

@@ -18,19 +18,20 @@ const YoutubeIframe = (
1818
width,
1919
videoId,
2020
playList,
21-
playListStartIndex = 0,
2221
play = false,
23-
onChangeState = _event => {},
24-
onReady = _event => {},
25-
onError = _err => {},
26-
onPlaybackQualityChange = _quality => {},
2722
mute = false,
2823
volume = 100,
29-
playbackRate = 1,
30-
onPlaybackRateChange = _playbackRate => {},
31-
initialPlayerParams = {},
3224
webViewStyle,
3325
webViewProps,
26+
playbackRate = 1,
27+
onError = _err => {},
28+
onReady = _event => {},
29+
playListStartIndex = 0,
30+
initialPlayerParams = {},
31+
forceAndroidAutoplay = false,
32+
onChangeState = _event => {},
33+
onPlaybackQualityChange = _quality => {},
34+
onPlaybackRateChange = _playbackRate => {},
3435
},
3536
ref,
3637
) => {
@@ -163,13 +164,22 @@ const YoutubeIframe = (
163164
return (
164165
<View style={{height, width}}>
165166
<WebView
166-
style={[styles.webView, webViewStyle]}
167167
ref={webViewRef}
168168
originWhitelist={['*']}
169-
source={{html: MAIN_SCRIPT(videoId, playList, initialPlayerParams)}}
170-
mediaPlaybackRequiresUserAction={false}
171-
allowsInlineMediaPlayback
172169
onMessage={onWebMessage}
170+
allowsInlineMediaPlayback
171+
style={[styles.webView, webViewStyle]}
172+
mediaPlaybackRequiresUserAction={false}
173+
allowsFullscreenVideo={!initialPlayerParams?.preventFullScreen}
174+
source={{html: MAIN_SCRIPT(videoId, playList, initialPlayerParams)}}
175+
userAgent={
176+
forceAndroidAutoplay
177+
? Platform.select({android: CUSTOM_USER_AGENT, ios: ''})
178+
: ''
179+
}
180+
onShouldStartLoadWithRequest={request => {
181+
return request.mainDocumentURL === 'about:blank';
182+
}}
173183
{...webViewProps}
174184
/>
175185
</View>

src/constants.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
export const PLAYER_STATES = {
2-
"-1": "unstarted",
3-
"0": "ended",
4-
"1": "playing",
5-
"2": "paused",
6-
"3": "buffering",
7-
"5": "video cued"
2+
'-1': 'unstarted',
3+
'0': 'ended',
4+
'1': 'playing',
5+
'2': 'paused',
6+
'3': 'buffering',
7+
'5': 'video cued',
88
};
99

1010
export const PLAYER_ERROR = {
11-
"2": "invalid_parameter",
12-
"5": "HTML5_error",
13-
"100": "video_not_found",
14-
"101": "embed_not_allowed",
15-
"150": "embed_not_allowed"
11+
'2': 'invalid_parameter',
12+
'5': 'HTML5_error',
13+
'100': 'video_not_found',
14+
'101': 'embed_not_allowed',
15+
'150': 'embed_not_allowed',
1616
};
17+
18+
export const CUSTOM_USER_AGENT =
19+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36';

0 commit comments

Comments
 (0)