A Video Player Based on React Native Video

react-native-rn-videoplayer

  • You can slide up and down to change the volume, screen brightness, drag the progress bar to display the time to be changed, full-screen switching, cache progress, double-click the video to pause, and other functions, based on react-native-video

  • ps: Android changes the brightness without obtaining advanced permissions. It only changes the current active, that is, the brightness of the current page. After changing the brightness, returning to another page will restore the original brightness.

  • Version 2.x requires react-native >= 0.60.0

  • Version 1.3.2 requires react-native <= 0.59.9

Short book address

Video preview

  • gif preview ios and android

     

Getting started

npm install react-native-rn-videoplayer --save

- - android

Open up android/app/src/main/java/[...]/MainActivity.java

+import android.content.Intent; 
+import android.content.res.Configuration;
public class MainActivity extends ReactActivity {

   ...

+ @Override
+      public void onConfigurationChanged(Configuration newConfig) {
+        super.onConfigurationChanged(newConfig);
+        Intent intent = new Intent("onConfigurationChanged");
+        intent.putExtra("newConfig", newConfig);
+        this.sendBroadcast(intent);
+    }
    ...
}

- - iOS

Add the following to your project’s AppDelegate.m:

+#import "Orientation.h"

@implementation AppDelegate

// ...

+- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
+  return [Orientation getOrientation];
+}

@end

RN >= 0.60

ios

  cd ios 

  pod install

Android.

Most of them are automatically linked. If you can’t find XX, you should link manually

  • settings.gradle
  rootProject.name = 'TestPack622'
  apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)

+    include ':react-native-linear-gradient'
+    project(':react-native-linear-gradient').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-linear-gradient/android')
+    include ':react-native-svg'
+    project(':react-native-svg').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-svg/android')
+    include ':react-native-orientation-locker'
+    project(':react-native-orientation-locker').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-orientation-locker/android')
+    include ':react-native-video'
+    project(':react-native-video').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-video/android-exoplayer')
+    include ':react-native-system-setting'
+    project(':react-native-system-setting').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-system-setting/android')

  include ':app'
  • MainApplication.java
+  import com.horcrux.svg.SvgPackage;
+  import com.BV.LinearGradient.LinearGradientPackage; // <--- This!
+  import org.wonday.orientation.OrientationPackage;
+  import com.ninty.system.setting.SystemSettingPackage;
+  import com.brentvatne.react.ReactVideoPackage;

  ···
 @Override
      protected List<ReactPackage> getPackages() {
        @SuppressWarnings("UnnecessaryLocalVariable")
        List<ReactPackage> packages = new PackageList(this).getPackages();
        // Packages that cannot be autolinked yet can be added manually here, for example:
+          packages.add(new LinearGradientPackage());
+          packages.add(new SvgPackage());
+          packages.add(new OrientationPackage());
+          packages.add(new SystemSettingPackage());
+          packages.add(new ReactVideoPackage());
        return packages;
      }
      ···
  • app/build.gradle
dependencies {
+    implementation project(':react-native-svg')
+    implementation project(':react-native-linear-gradient')
+    implementation project(':react-native-orientation-locker')
+    implementation project(':react-native-system-setting')
+    implementation project(':react-native-video')
}

RN <= 0.59

react-native link react-native-linear-gradient
react-native link react-native-orientation-locker
react-native link react-native-svg
react-native link react-native-system-setting
react-native link react-native-video

Android

  1. Append the following lines to android/settings.gradle:
include ':react-native-rn-videoplayer'
project(':react-native-rn-videoplayer').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-rn-videoplayer/android')
  1. Insert the following lines inside the dependencies block in android/app/build.gradle:
  compile project(':react-native-rn-videoplayer')
  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add
import com.ngxu.videoplayer.RNVideoplayerPackage;

new RNVideoplayerPackage() //to the list returned by the `getPackages()` method

Usage

import Videoplayer from 'react-native-rn-videoplayer';

< VideoPlayer 
url = { "https://xxxxx.mp4" } 
navigation = { this . Props . Navigation } //Route the return button for small screen playback 
ref = { ( ref ) => this . Player = ref } 
poster = { "http:XXX.jpg" } //Video cover 
/ >

api

  • url video address

  • continuous Whether to enable the selection function when full screen is suitable for serials, the default is false

  continuous={true}
  • renderAllSeenList The list of episodes displayed after clicking the selection
···
<VideoPlayer
  url={"https://xxxxx.mp4"}
  ref={(ref)=>this.player=ref}
  renderAllSeenList={this.renderAllSeenList}
/>

···
  renderAllSeenList = () => (
  <View style={{ width: height / 2.5, backgroundColor: "rgba(0,0,0,0.6)", position: "absolute", top: 0, bottom: 0, right: 0, }}>
      <ScrollView>
        <Button 
          onPress={()=> { 
              const  newdata  =  this . state . data 
                    newdata . index  =  newindex // Episode number 
              //Update episode number and restart playing 
              this . setState ( {  data : newdata  } ,  ( )  =>  {  this . player . rePlay ( )  } ) 
          } }

        />  
      </ScrollView>      

    </View>
  )
  • NextBtnFun The method of the next episode button in full screen. When it is the last episode, the value should be changed to false, and the button should be grayed out
const  { data }  =  this . state 
//data.index is the number of episodes 
//When the 
number of episodes currently played is the same as the total number of episodes, reset nextBtnFun to false nextBtnFun = { 
  data.index == data.datalist[data.datalist.length - 1].num - 1 ? false : this.nextBtnFun
  }
  • The icon of the favorite button in the upper right corner of storeComponent
  storeComponent={()=><Image/>}
  • moreSetting The icon of the more button in the upper right corner
  moreSetting={()=><Image/>}
  • setPaused play pause
  this . player . setPaused ( true ) //true pause; false play;

  <VideoPlayer
   ref={(ref)=>this.player=ref}
  >
  • reLoad
  this.player.reLoad()

  <VideoPlayer
   ref={(ref)=>this.player=ref}
  >
  • onSmallBack When the video is a small window, click the callback of the back button. You can add the function func to return to the previous page here.
  • onStore Click the callback func of the favorite button in the upper right corner
  • onMoreFun Click the callback func of the More button in the upper right corner
  • onWindowChange callback func for window change
  < VideoPlayer 
    onWindowChange = { ( e ) => { } } //e: "full" full screen "small" small screen 
  >
  • onLoad The callback that the video can start playing after loading successfully inherits react-native-veideo
  • onSeek The callback after adjusting the progress inherits the onSeek of react-native-video
  • onEnd The callback after playback inherits the onSeek of react-native-video
  • Whether onBuffer is waiting to be loaded, the video can be fetched and it is stuck showing loading or the video can be played to hide the loading callback. Inherit the onBuffer of react-native-video
  • poster video cover image The cover image displayed when the video cannot be played is not the loading frame inherits the react-native-video poster eg:poster={" http://i1.hdslb.com/bfs/archive/784013a39c59aede1ee9e775ec271790dfd7dc4b.jpg@ 320w_200h.jpg"}
  • … Inherit all the methods and properties of react-native-video
  • changeWindows() switch full screen or small screen
  • changeWindows(boolean) true full screen, false small screen
  • Example:
  <VideoPlayer ref={(ref)=>this.player=ref}/>
  this.player.changeWindows(true); // 全屏

Exposure method

import {NgxuSetting} from 'react-native-rn-videoplayer'

Show or hide the virtual buttons at the bottom of Android

const Setting = new NgxuSetting()
Setting.hideAndroidBottom()
Setting.showAndroidBottom()

Get the phone system brightness

const Setting = new NgxuSetting()
Setting.getBrightness((e)=>{consoloe.log(e)})

Change ios system brightness android current app brightness

  • Android only changes the current active brightness and does not modify the system brightness. Modifying the system brightness is very troublesome. It requires the user to manually open the phone settings, app permission settings, and manually open the advanced permissions of “Allow to modify system settings”
  • ios is to change the system brightness, you can
   const Setting = new NgxuSetting()
   Setting.SetBrightness(1)//0-1之间

Download Details:

Author: ngxu

Live Demo: https://streamja.com/embed/ZJk5P

GitHub: https://github.com/ngxu/react-native-rn-videoplayer

#react-native #react #mobile-apps

A Video Player Based on React Native Video
21.15 GEEK