14

I am trying to play Youtube videos in react native android/ios app. I have defined a webview:

<WebView
    style={styles.frame}
    url={this.props.url}
    renderLoading={this.renderLoading}
    renderError={this.renderError}
    automaticallyAdjustContentInsets={false}
/>

And passing the url of the video I want to play:

this.navigate('Play', 'https://www.youtube.com/watch?v=RJa4kG1N3d0')

But this displays the whole youtube page in the webview including the comments section.

enter image description here

I want to display only the video section and not the comment section. Is there anything missing in the url?

masud_moni
  • 1,121
  • 16
  • 33
triandicAnt
  • 1,328
  • 2
  • 15
  • 40
  • 1
    just a comment, Google will block your app if its playing videos outside of youtube. – giannisf Jul 01 '16 at 20:01
  • 1
    Make sure your video in Android doesn't keep playing in the background when you lock the device. Otherwise the app will be rejected from the Google Play Store. – Agu Dondo Aug 04 '17 at 17:23

7 Answers7

20

The easiest way to embed YouTube in the React Native iOS device is to use the <WebView>. All you need to do is to replace watch?v= with embed. This will not work with Android.

Example:

<WebView
        style={{flex:1}}
        javaScriptEnabled={true}
        source={{uri: 'https://www.youtube.com/embed/ZZ5LpwO-An4?rel=0&autoplay=0&showinfo=0&controls=0'}}
/>
Community
  • 1
  • 1
vincent mathew
  • 4,278
  • 3
  • 27
  • 34
  • 1
    Working for me in both android emulator and device (htc desire), just using flex did not work. (had to specify height and width) – Adrien Joly Oct 23 '17 at 15:58
  • 3
    My app was rejected from the Google Play store using this method, check here for alternate solutions -- https://stackoverflow.com/questions/36382935/ – Clay Apr 21 '18 at 17:59
6

This code worked for me

<WebView
        style={{ marginTop: 20, width: 320, height: 230 }}
        javaScriptEnabled={true}
        domStorageEnabled={true}
        source={{ uri: "https://www.youtube.com/embed/-ZZPOXn6_9w" }}
      />
Nick.K
  • 320
  • 6
  • 11
5

I had your problem. You want your users to pass video tutorial as props, but a naive user don't know what is embedded link, he will just copy and paste an URL from browser and paste it, which will not be the embedded one, but you can convert it. See this example:

Original Video :- https://www.youtube.com/watch?v=qaiLSpqeBHY
Embedded Video :- https://www.youtube.com/embed/qaiLSpqeBHY

Lets see how you can convert it:

const OriginalVideo = "https://www.youtube.com/watch?v=qaiLSpqeBHY"

const SplitedVideo = OriginalVideo.split("watch?v=")

const Embed = SplitedVideo.join("embed/")

console.log(Embed)

Here is your embedded video converted from original video URL.

Live Example:-

   componentDidMount() {
        const Video = this.props.navigation.getParam("Video");

        const MyVideo = Video.split("watch?v=");

        const EmbededVideo = MyVideo.join("embed/");

        this.setState({
         Video: EmbededVideo
        });
        }
Ali Haider
  • 76
  • 1
  • 3
4

I think you can load the embed html from youtube with the video, directly into your react native webview. Rather than navigating to a url you would instead set the source attribute of the WebView to your html, like so:

<WebView source={{ html: "HTML here" }} 
.../>

based on this stack overflow post describing how to load a youtube iframe into an normal android webview you could replace "HTML here" with the actual html so it would look like:

<WebView source={{ html: "<html><body>Look Ma' a video! <br /> <iframe width="560" height="315" src="https://www.youtube.com/embed/RJa4kG1N3d0" frameborder="0" allowfullscreen></iframe></body></html>" }} 
.../>

You can get the directions for getting the embed link for a youtube video here.

Community
  • 1
  • 1
Dr. Nitpick
  • 1,662
  • 1
  • 12
  • 16
0
<WebView
  style={{ flex: 1, aspectRatio: 16 / 9 }}
  source={{ uri: 'https://www.youtube.com/embed/LXb3EKWsInQ?rel=0&autoplay=1&showinfo=0' }}
  allowsInlineMediaPlayback={true}
  mediaPlaybackRequiresUserAction={Platform.OS !== 'android' || Platform.Version >= 17 ? false : undefined}
  userAgent="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"
/>
  • Support autoplay (without auto fullscreen)
  • Tested both iOs and Android
l2aelba
  • 21,591
  • 22
  • 102
  • 138
0

I recommend to use react-native-youtube-iframe

Code: https://lonelycpp.github.io/react-native-youtube-iframe/basic-usage

import React, { useState, useCallback, useRef } from "react";
import { Button, View, Alert } from "react-native";
import YoutubePlayer from "react-native-youtube-iframe";

export default function App() {
  const [playing, setPlaying] = useState(false);

  const onStateChange = useCallback((state) => {
    if (state === "ended") {
      setPlaying(false);
      Alert.alert("video has finished playing!");
    }
  }, []);

  const togglePlaying = useCallback(() => {
    setPlaying((prev) => !prev);
  }, []);

  return (
    <View>
      <YoutubePlayer
        height={300}
        play={playing}
        videoId={"iee2TATGMyI"}
        onChangeState={onStateChange}
      />
      <Button title={playing ? "pause" : "play"} onPress={togglePlaying} />
    </View>
  );
}
Vasyl Nahuliak
  • 1,912
  • 2
  • 14
  • 32
0

User Javascript replace()

<WebView
   mediaPlaybackRequiresUserAction={true}
   source={{
   uri: video_url.replace("watch?v=", "embed/"),
  }}
  style={styles.video}
  javaScriptEnabled={true}
  />