Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to autoplay youtube embedded video in react native?

I am trying to autoplay the youtube embedded video in my react native app.

here is my code,

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>
    );
}

but it's not working, I have a playIcon custom button, if I click that button then youtube video should play. I don't know where I am wrong in the code.

like image 805
khalifathul Avatar asked Mar 09 '18 05:03

khalifathul


1 Answers

You can user react-native-youtube for embedding youtube video to your react native project. You can configure the property to autoplay the video.

<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 }}
/>
like image 78
DEVENDRA SINGH SHEKHAWAT Avatar answered Oct 14 '22 04:10

DEVENDRA SINGH SHEKHAWAT