react-native - 如何在 React Native 中自动播放 youtube 嵌入的视频?

标签 react-native youtube autoplay

我正在尝试在我的 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 视频。我不知道我在代码中哪里错了。

最佳答案

您可以使用 react-native-youtube 将 youtube 视频嵌入到您的 native 项目中。您可以配置该属性以自动播放视频。

<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 }}
/>

关于react-native - 如何在 React Native 中自动播放 youtube 嵌入的视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49187085/

相关文章:

PHP学院教程-查询方法

YouTube 数据 API 为没有 channel 的用户返回 channel

android - 自动播放视频( ListView )

jquery - 通过 jQuery 附加时,视频自动播放在 chrome 和 IE 上不起作用

react-native - 无法加载脚本。确保您正在运行 Metro 服务器(运行 'react-native start' )或您的包 'index.android.bundle'

javascript - 如何从 React Native 输入字段获取值?

android - 错误: Requiring module "node_modules\react-native-reanimated\src\Animated.js"

objective-c - Webview/Webkit 未按预期工作

reactjs - react native "Value for longitude cannot be cast from String to Double"

html - autoplay=1 不适用于 &lt;iframe&gt; 在移动设备中播放视频