Youtube iframe视频在本机Web视图中不起作用

时间:2018-11-23 16:35:26

标签: android react-native youtube-api android-webview instagram-api

我在使用react-native-webview播放包装在iframe中的youtube或instagram视频时遇到问题。问题仅在Android中。 iOS运行正常。

因此,当我将iframe放入Webview时,它看起来还不错。直到我单击播放按钮。视频开始加载,所有本机按钮均可用,并且声音正常,但电影为黑色。声音和停止/播放按钮起作用。

依赖项:

react-native-cli: 2.0.1
react-native: 0.57.5
"react-native-webview": "^2.12.1"

我准备了一个简单的演示,其中视频不起作用:

  render() {
    return (
      <WebView
        originWhitelist={['*']}
        useWebKit={false}
        source={{
          html: `
          XX
          <div>
            <iframe width="320" height="240" src="https://www.youtube.com/embed/li8yILhFFZM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> 
          </div> `

        }}
      />

    );
  }
}

和问题的屏幕截图:

enter image description here

谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

或者,您可以简单地将URI与react-native的默认webview一起使用,与android兼容。

   <View style={styles.cardRounded}>
    <WebView
      mediaPlaybackRequiresUserAction={true}
      style={{ height:240, width:320,alignSelf:"center",alignContent:"center"}}
      source={{uri: 'https://www.youtube.com/embed/li8yILhFFZM?rel=0' }}
    />
   </View>

let styles = StyleSheet.create({
    cardRounded: {
        paddingEnd:0,  paddingTop: 0 , paddingBottom: 0, paddingStart: 0, padding:0,
        borderRadius: 12, alignItems:"center", backgroundColor:#000000,flex:0, height:240,
    },

});