React Native Integrate YouTube Video in Android iOS App Example

YouTube is one of the highest uses online video sharing platform available on internet. On a particular time every mobile application developer needs to user YouTube videos in their mobile application easily. So in react native there are a lot’s of ways to integrate YouTube video but most of them is unreliable. But using react-native-youtube custom component we can easily integrate YouTube videos in our android iOS mobile application. We can also add multiple videos in array format and they run one by one and also add YouTube video playlist using the react-native-youtube library. So in this tutorial we would React Native Integrate YouTube Video in Android iOS App Example Tutorial.

Note: We have to use YouTube video ID in our tutorial. You can find any YouTube video ID in Video URL. See the below screenshot, After the = sign you can find any YouTube video ID.

Contents in this project React Native Integrate YouTube Video in Android iOS App Example Tutorial:

1. Before getting started to coding part we have to manually install the react-native-youtube library in our react native project. So open your react native project Root directory in Command Prompt or Terminal and execute below command.

Screenshot of Terminal:

Screenshot of Terminal after finish installing:

2. Now the installing part is completed. Next part is to link the newly installed YouTube library to your project index. So again open your react native project root directory in Command Prompt or Terminal and execute below link command to link the YouTube library.

Screenshot of Terminal:

3. Generate API Key for Library:

1. The react-native-youtube library is a professional library that works on YouTube basic terms. So In order to use the library we must generate the API key form Google Developer Console and use the API key in our react native project. Without the API key this won’t work. Go To Google Developer Console. Login using your Google Gmail account.

2. Select any old created project from Developer console or you can also create a new project. After done creating project goto APIs & Services -> Credentials.

3. Under Create Credentials Select API Key.

4. If will take a moment and now your API key will be visible on your computer screen. Simply Copy the API key on someplace safe because we will use it later.

5. Now the API creating part is done. Next step is to enable the YouTube Data using in current project. So when we use this API in our project it won’t display us an error. So goto Library.

6. Scroll down on the Library section and select YouTube Data API v3 .

7. Press the Enable button to Enable YouTube data api in your current project.

8. It will take some time then enable the YouTube Data API v3.

Here you go. Now we would use this API key into our react native project. So let’s get started coding.

4. Start Coding for App:

1. Open your react native project’s App.js file and Import StyleSheet, Text, PixelRatio, TouchableOpacity, View and Dimensions component.

2. Import YouTube component from react-native-youtube library.

3. Create our main export default class named as App extends Component. This would be our main View class.

4. Create constructor() in your class. Inside the constructor we would make 3 States named as isLoading, loadingStatus and fullscreen.

  1. isLoading : Used to check the Video status like video is loading or video is ready to play.
  2. loadingStatus : Holds 2 values buffering and Playing depends on status given by component.
  3. fullscreen : Used to manually enable and disable YouTube video full screen.

5. Create a main Container View in render’s return block. We will put our all the components in this Root View.

6. Create YouTube component in Root View.

  1. ref : Is used to mange as reference of Video to enable previous and next functionality.
  2. apiKey : You have to put here you own API key.
  3. playlistId : You can pass here YouTube video playlist id to show video from Playlist.
  4. videoId : Pass here single YouTube video ID.
  5. videoIds : Pass here multiple ID’s in array format to enable showing multiple videos.
  6. fullscreen : To enable and disable full screen mode.
  7. style : Used to Style the YouTube video screen. We are setting up the height of Player according to width of screen in 16:9 ratio.
  8. onReady : To manage YouTube video status.

7. Creating Next, Previous buttons and Skip to 30 seconds and Skip to 5 minutes buttons with Video status.

8. Creating Style.

9. Complete Source Code for App.js file class:

Screenshots:

React Native Integrate YouTube Video in Android iOS App Example

Leave a Reply

Your email address will not be published.