Skip to main content

Video Streaming in Your React Native App

Video Streaming in Your React Native App

Photo by Zach Meaney on Unsplash

I have recently been working with the react-native-video library to integrate videos within the app. I must say, this is a very impressive library from the react native community with lots of contributors and users.

Key Features

Some of the key features of this library include:

  • Local and remote files playback support.
  • Selection of audio and text tracks with captions.
  • Configurable rate (increase or decrease speed of video).
  • Audio playback when the app is running in the background.
  • External playback.
  • And ofcourse support for both iOS and Android devices.

These features make this library a solid pick for video streaming in react native apps.

Setup

The setup for both iOS and Android is quite simple.

Run the following command to install the react-native-video package

npm install --save react-native-video

Link react-native-video library with both the iOS and Android dependencies

react-native link react-native-video

With these two commands you are all set and ready to start coding your video component.

Getting Started with using <Video> component

I have created a VideoComponent.js which will be used to render the video. Anytime we want the video to stream within this application, we could use the VideoComponent.

You can observe that the renderVideo() method invokes the <Video> component from the react-native-video library.

For this component to render we would need to provide the source of the video and a style for the player to playback within our app.

In the example below we are providing a mp4 file from the assets folder and a basic style for the player. All other props that come with the <Video> are optional and configured based on our needs.

import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { View, StyleSheet } from 'react-native'
import Video from 'react-native-video'

export default class VideoComponent extends React.Component {

  renderVideo () {
      return(
        <Video
          source={require('./assets/Piano_Playing_Close.mp4')}
          style={{ width: 800, height: 800 }}
          muted={true}
          repeat={true}
          resizeMode={"cover"}
          volume={1.0}
          rate={1.0}
          ignoreSilentSwitch={"obey"}

        />
      )
  }

  render () {
    return (
      <View>
        {this.renderVideo()}
      </View>
    )
  }
}

// Later on in your styles..
var styles = StyleSheet.create({
  backgroundVideo: {
    position: 'absolute',
    top: 0,
    left: 0,
    bottom: 0,
    right: 0,
  },
});
Now our VideoComponent is ready and we can invoke this from the any page/screen. For our example, I am going to invoke this from the App.js file which is the starter page of our app.
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import VideoComponent from './VideoComponent'

export default class App extends Component<Props> {
  render() {
    return (
        <View>
        <VideoComponent />
        </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  }
});
The render() methods invokes the <VideoComponent /> and with that we are all set to get this running.

Run the code for either iOS or android

//iOS
react-native run-ios
// Android
react-native run-android

The emulator now opens up with the video streaming within the app.

Gif showing the emulator streaming video

Useful Props

Once we have the basic video streaming integrated, we can play around with the props that come with this library.

We can increase or decrease volume, mute the audio, ensure audio playback when the app is backgrounded, select audio/text tracks etc..

One thing to note is the new addition of the ignoreSilentSwitch prop. In our example I have set it to “obey”. This is specific to iOS devices and refers to the hardware silent switch on the user’s device. If you do notice that the video is streaming without any audio on the device, the device silent switch maybe enabled. You could override it and set the prop to “ignore”

Keep in mind that setting the ignoreSilentSwitch to “ignore” may not be the ideal solution since it would not make the users happy.

Remote File Playback

In the previous example we saw a file playing back from the local assets folder. It is simple to integrate the same library to stream videos from a remote url as well.

Modify the source to pull the video from a url instead as shown below.

source={{uri: '<REMOTE URL>'}}

For more information on the library checkout the documentation on github. Hope you enjoy integrating video streaming into your react native app.

Comments

Popular Posts

React Native: How To Open App Settings Page

  React Native: How To Open App Settings Page React Native has a Linking module to manage the interaction with other applications in the device. In this article, I will explain how you can open “Application Setting Page” of the device using. Sample Case Before I explain how to do this, I’ll talk about an example case where we might need this: You need to get location permission within the application from a user using an iOS operating system and you asked for permission. But user did not agree to grant this permission the first time. In this case, you cannot ask your user for location permission the second time. In this case, the only thing to do is to direct the user to the settings page of our application and make it manually allow it. Now that we have explained our example situation, we can move on to how we can redirect our user to the “settings page of our application”. Open App Settings Page iOS If our user is using an iOS operating system, we can handle the whole process wit...

Integrate the FaceID or TouchID Authentication in iOS Apps - Swift

FaceID or TouchID Authentication Dear Folks, today I am going to explain the proper way to integrate the Face ID or Touch ID in iOS App. Face ID and Touch ID are secure, familiar authentication methods that people trust. Follow below steps: Step 1: Add NSFaceIDUsageDescription Key in info.plist file <key>NSFaceIDUsageDescription</key> <string>This app requires Face ID permission to authenticate using Face recognition.</string> Step 2: Create a swift file with any name but it would be better to related to its functionality, Like BioMetrixFile.swift Step 3: In BioMetrixFile write below codes and follow instructions. But before proceeding further import LocalAuthentication class in BioMetrixFile. Now, create typealias for success or failure blocks to well manage Authentication Success or Failure. public typealias AuthenticationSuccess = (() -> ()) public typealias AuthenticationFailure = ((AuthenticationError) -> ()) Now written all s...

Add Custom Checkbox on UITableViewCell or UITableView in Swift 4

Add Custom Checkbox Introduction to how to add custom checkbox on UITableViewCell or UITableView in swift 4 In this tutorial, we are going to learn about how can we create or add custom checkbox on UITableViewCell or UITableView in swift and perform IBAction on our custom checkbox placed on UITableViewCell or UITableView in swift 4. We will place UIButton on UITableViewCell that will act as checkbox for our UITableViewCell in UITableView using swift 4. In the end of this tutorial, we will have output as shown in below picture Learn how to create checkbox in swift from  HERE Steps to create or add custom checkbox on UITableViewCell or UITableView Step 1 :  Create a new Xcode project and select  "Single View App"  template. Name  project as  "addCheckBoxonTable-Tutorial" . Step 2:  Open  "Main.storyboard"  file and drag UITableView object on to the view. Then we will add constraints to UITableView so that it fits...