如何在React Native中自动播放嵌入的YouTube视频?

11

我想在我的React Native应用中自动播放嵌入的YouTube视频。

这是我的代码:

render() {
    const { data } = this.props;
    const { loading } = this.state;
    return (
        <View>
            <CloseButton onTrigger={this.closeModal.bind(this)} />
            <PlayIcon onTrigger={this.playVideo} />
            <Image source={{uri:data.src}} resize='contain' style={{height:250}} />
           <Image 
                source={require('../../assets/img/grediant.png')}
                resizeMode='cover' style={styles.gradientImage} />
            <ContentWidget style={styles.infoContentTwo}>
                <MyAppText style={{color:'white'}}>{data.title}</MyAppText>
            </ContentWidget>
            {this.state.openVideo && <View style={styles.webViewStyle}>
                <WebView style={{height:250}} source={{uri:`${data.video}?autoplay=1`}} fullScreen={true} />
            </View>}
            {loading && <Spinner />}
        </View>
    );
}

但它没有起作用,我有一个playIcon自定义按钮,如果我点击该按钮,则应播放YouTube视频。我不知道代码哪里出了问题。


我也在寻找同样的解决方案......即使将mediaPlaybackRequiresUserAction = false设置为false,它仍然无效。 - Akhilesh Sinha
我也在寻找同样的解决方案……因为设置mediaPlaybackRequiresUserAction = false没有起作用。 - Akhilesh Sinha
1个回答

1
你可以使用 react-native-youtube 在 React Native 项目中嵌入 YouTube 视频。你可以配置属性来自动播放视频。

<YouTube
  videoId="KVZ-P-ZI6W4"   // The YouTube video ID
  play={true}             // control playback of video with true/false 
  onReady={e => this.setState({ isReady: true })}
  onChangeState={e => this.setState({ status: e.state })}
  onChangeQuality={e => this.setState({ quality: e.quality })}
  onError={e => this.setState({ error: e.error })}
  style={{ alignSelf: 'stretch', height: 300 }}
/>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接