React Native Video Library to Play Video in Android and IOS

React Native Play Video

This is an example of React Native Video to Play Any Video in Android and IOS. There are many libraries available to play any video in React Native but React Native Video is the basic and one of the react-native-community members which means it is accepted by the React Native development team so I personally like to use it.

React Native Video provides all the props which you need while playing a video and we can control them easily. React Native Video does not provide the inbuilt controls to play /Pause or Seek the video as they have provided the props for the full access. If you are interested to design your own controls then your welcome else you can use a supporting library called react-native-media-controls. It provides you all the functional controls which you need for a standard video player.

Loading Overlay

Play / Pause

Seek

FullScreen

To Play a Video in React Native

To play a video in React Native we are going to use Video and MediaControls components from react-native-video and react-native-media-controls libraries.

Video component

<Video
    onEnd={this.onEnd}
    onLoad={this.onLoad}
    onLoadStart={this.onLoadStart}
    onProgress={this.onProgress}
    paused={this.state.paused}
    ref={videoPlayer => (this.videoPlayer = videoPlayer)}
    resizeMode={this.state.screenType}
    onFullScreen={this.state.isFullScreen}
    source={{ uri: 'https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4' }}
    style={styles.mediaPlayer}
    volume={10}
/>

MediaControls Component

<MediaControls
    duration={this.state.duration}
    isLoading={this.state.isLoading}
    mainColor="#333"
    onFullScreen={this.onFullScreen}
    onPaused={this.onPaused}
    onReplay={this.onReplay}
    onSeek={this.onSeek}
    onSeeking={this.onSeeking}
    playerState={this.state.playerState}
    progress={this.state.currentTime}
    toolbar={this.renderToolbar()}
/>

For this example, We are going to play a simple video with MediaControls. So let’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To use Video and MediaControls we need to install react-native-video and react-native-media-controls dependencies.

To install this open the terminal and jump into your project using

cd ProjectName

Run the following command to install

npm install --save react-native-video
npm install --save react-native-media-controls

This command will copy all the dependency into your node_module directory.

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the library but need to install pods. So to install pods use

cd ios && pod install && cd ..

Code

Now Open App.js in any code editor and replace the code with the following code

App.js

/*Example of React Native Video*/
import React, { Component } from 'react';
//Import React
import { Platform, StyleSheet, Text, View } from 'react-native';
//Import Basic React Native Component
import Video from 'react-native-video';
//Import React Native Video to play video
import MediaControls, { PLAYER_STATES } from 'react-native-media-controls';
//Media Controls to control Play/Pause/Seek and full screen

class App extends Component {
  videoPlayer;

  constructor(props) {
    super(props);
    this.state = {
      currentTime: 0,
      duration: 0,
      isFullScreen: false,
      isLoading: true,
      paused: false,
      playerState: PLAYER_STATES.PLAYING,
      screenType: 'content',
    };
  }

  onSeek = seek => {
    //Handler for change in seekbar
    this.videoPlayer.seek(seek);
  };

  onPaused = playerState => {
    //Handler for Video Pause
    this.setState({
      paused: !this.state.paused,
      playerState,
    });
  };

  onReplay = () => {
    //Handler for Replay
    this.setState({ playerState: PLAYER_STATES.PLAYING });
    this.videoPlayer.seek(0);
  };

  onProgress = data => {
    const { isLoading, playerState } = this.state;
    // Video Player will continue progress even if the video already ended
    if (!isLoading && playerState !== PLAYER_STATES.ENDED) {
      this.setState({ currentTime: data.currentTime });
    }
  };
  
  onLoad = data => this.setState({ duration: data.duration, isLoading: false });
  
  onLoadStart = data => this.setState({ isLoading: true });
  
  onEnd = () => this.setState({ playerState: PLAYER_STATES.ENDED });
  
  onError = () => alert('Oh! ', error);
  
  exitFullScreen = () => {
    alert('Exit full screen');
  };
  
  enterFullScreen = () => {};
  
  onFullScreen = () => {
    if (this.state.screenType == 'content')
      this.setState({ screenType: 'cover' });
    else this.setState({ screenType: 'content' });
  };
  renderToolbar = () => (
    <View>
      <Text> toolbar </Text>
    </View>
  );
  onSeeking = currentTime => this.setState({ currentTime });

  render() {
    return (
      <View style={styles.container}>
        <Video
          onEnd={this.onEnd}
          onLoad={this.onLoad}
          onLoadStart={this.onLoadStart}
          onProgress={this.onProgress}
          paused={this.state.paused}
          ref={videoPlayer => (this.videoPlayer = videoPlayer)}
          resizeMode={this.state.screenType}
          onFullScreen={this.state.isFullScreen}
          source={{ uri: 'https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4' }}
          style={styles.mediaPlayer}
          volume={10}
        />
        <MediaControls
          duration={this.state.duration}
          isLoading={this.state.isLoading}
          mainColor="#333"
          onFullScreen={this.onFullScreen}
          onPaused={this.onPaused}
          onReplay={this.onReplay}
          onSeek={this.onSeek}
          onSeeking={this.onSeeking}
          playerState={this.state.playerState}
          progress={this.state.currentTime}
          toolbar={this.renderToolbar()}
        />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  toolbar: {
    marginTop: 30,
    backgroundColor: 'white',
    padding: 10,
    borderRadius: 5,
  },
  mediaPlayer: {
    position: 'absolute',
    top: 0,
    left: 0,
    bottom: 0,
    right: 0,
    backgroundColor: 'black',
  },
});
export default App;

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Output Screenshots

IOS

      

Android

      

This is how you can Play Video in Android and IOS devices using React Native Video Library. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂


Related Posts

31 thoughts on “React Native Video Library to Play Video in Android and IOS”

  1. hello. i did every step in this tutorial and i run into problem. position of the video is broken. can you help me out? Ty in advance. I can send you PS of simulator to see what i am talking about.

    Reply
  2. Hello sir,
    Is it possible to implement the below logic
    When the user stops video due to some reasons,is it possible to show the video from where he left off.[ I mean duration ]

    Reply
    • You need to access the local directory for the same. and then after getting the list from there, you have to create the list of videos. I haven’t done that before so not sure how to do that but you can use RNFS for that.

      Reply
  3. Hello, The latest version gives follow warning during the build: The following packages use deprecated “rnpm” config that will stop working from next release: react-native-video

    I tried replaceing rnpm from package.json to newly created react-native.config.js but didn’t work.

    Also, checked the issues forum of the library but couldn’t find a solution. Will this affect the app in any way in future?

    Reply
  4. Whenever I change orientation to landscape I wanted to make video full-screen take full width and height. But it doesn’t work. It shows with header tab and bottom tab from react navigation

    Reply
  5. Hi, 2 peoples says :
    – “On fullscreen it is only stretching the video not making it in landscape mode. Video is not at all going to landscape mode.”
    – “It shows with header tab and bottom tab from react navigation”.

    Your example work but not in component with header table, bottom tab from react native navigation we cant do it,

    Can we combine react native orientation method’s into “onFullScreen={this.onFullScreen}” ?

    And your website’s help me greatifull 🙂 thanks again to sharing your exp and knowledge “about react” 🙂

    Reply
  6. I have this error :

    error: Error: Unable to resolve module `react-native-slider` from `node_modules\react-native-media-controls\dist\react-native-media-controls.cjs.production.min.js`: react-native-slider could not be found within the project.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.