- Notifications
You must be signed in to change notification settings - Fork0
A <Video /> component for react-native
License
justhive/react-native-video
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A<Video> component for react-native, as seen inreact-native-login!
Requires react-native >= 0.40.0, for RN support of 0.19.0 - 0.39.0 please use a pre 1.0 version.
Runnpm i -S react-native-video
Runreact-native link to link the react-native-video library.
If you would like to allow other apps to play music over your video component, add:
AppDelegate.m
#import<AVFoundation/AVFoundation.h>// import- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ ... [[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryAmbienterror:nil];// allow ...}
Note: you can also use theignoreSilentSwitch prop, shown below.
Runreact-native link to link the react-native-video library.
Or if you have trouble, make the following additions to the given files manually:
android/settings.gradle
include':react-native-video'project(':react-native-video').projectDir=newFile(rootProject.projectDir,'../node_modules/react-native-video/android')
android/app/build.gradle
dependencies {... compile project(':react-native-video')}
MainApplication.java
On top, where imports are:
importcom.brentvatne.react.ReactVideoPackage;
Add theReactVideoPackage class to your list of exported packages.
@OverrideprotectedList<ReactPackage>getPackages() {returnArrays.asList(newMainReactPackage(),newReactVideoPackage() );}
Make the following additions to the given files manually:
windows/myapp.sln
Add theReactNativeVideo project to your solution.
- Open the solution in Visual Studio 2015
- Right-click Solution icon in Solution Explorer > Add > Existing Project...
UWP: Selectnode_modules\react-native-video\windows\ReactNativeVideo\ReactNativeVideo.csprojWPF: Selectnode_modules\react-native-video\windows\ReactNativeVideo.Net46\ReactNativeVideo.Net46.csproj
windows/myapp/myapp.csproj
Add a reference toReactNativeVideo to your main application project. From Visual Studio 2015:
- Right-click main application project > Add > Reference...
UWP: CheckReactNativeVideo from Solution Projects.WPF: CheckReactNativeVideo.Net46 from Solution Projects.
MainPage.cs
Add theReactVideoPackage class to your list of exported packages.
usingReactNative;usingReactNative.Modules.Core;usingReactNative.Shell;usingReactNativeVideo;// <-- Add thisusingSystem.Collections.Generic;...public override List<IReactPackage>Packages{get{return new List<IReactPackage>{newMainReactPackage(),newReactVideoPackage(),// <-- Add this};}}...
// Within your render function, assuming you have a file called// "background.mp4" in your project. You can include multiple videos// on a single screen if you like.<Videosource={{uri:"background"}}// Can be a URL or a local file.ref={(ref)=>{this.player=ref}}// Store referencerate={1.0}// 0 is paused, 1 is normal.volume={1.0}// 0 is muted, 1 is normal.muted={false}// Mutes the audio entirely.paused={false}// Pauses playback entirely.resizeMode="cover"// Fill the whole screen at aspect ratio.*repeat={true}// Repeat forever.playInBackground={false}// Audio continues to play when app entering background.playWhenInactive={false}// [iOS] Video continues to play when control or notification center are shown.ignoreSilentSwitch={"ignore"}// [iOS] ignore | obey - When 'ignore', audio will still play with the iOS hard silent switch set to silent. When 'obey', audio will toggle with the switch. When not specified, will inherit audio settings as usual.progressUpdateInterval={250.0}// [iOS] Interval to fire onProgress (default to ~250ms)onLoadStart={this.loadStart}// Callback when video starts to loadonLoad={this.setDuration}// Callback when video loadsonProgress={this.setTime}// Callback every ~250ms with currentTimeonEnd={this.onEnd}// Callback when playback finishesonError={this.videoError}// Callback when video cannot be loadedonBuffer={this.onBuffer}// Callback when remote video is bufferingonTimedMetadata={this.onTimedMetadata}// Callback when the stream receive some metadatastyle={styles.backgroundVideo}/>// Later to trigger fullscreenthis.player.presentFullscreenPlayer()// To set video position in seconds (seek)this.player.seek(0)// Later on in your styles..varstyles=StyleSheet.create({backgroundVideo:{position:'absolute',top:0,left:0,bottom:0,right:0,},});
- For iOS you also need to specify muted for this to work
// Within your render function, assuming you have a file called// "background.mp4" in your expansion file. Just add your main and (if applicable) patch version<Videosource={{uri:"background",mainVer:1,patchVer:0}}// Looks for .mp4 file (background.mp4) in the given expansion version.rate={1.0}// 0 is paused, 1 is normal.volume={1.0}// 0 is muted, 1 is normal.muted={false}// Mutes the audio entirely.paused={false}// Pauses playback entirely.resizeMode="cover"// Fill the whole screen at aspect ratio.repeat={true}// Repeat forever.onLoadStart={this.loadStart}// Callback when video starts to loadonLoad={this.setDuration}// Callback when video loadsonProgress={this.setTime}// Callback every ~250ms with currentTimeonEnd={this.onEnd}// Callback when playback finishesonError={this.videoError}// Callback when video cannot be loadedstyle={styles.backgroundVideo}/>// Later on in your styles..varstyles=Stylesheet.create({backgroundVideo:{position:'absolute',top:0,left:0,bottom:0,right:0,},});
The asset systemintroduced in RN0.14 allows loading image resources shared across iOS and Android without touching native code. As of RN0.31the same is true of mp4 video assets for Android. As ofRN0.33 iOS is also supported. Requiresreact-native-video@0.9.0.
<Video repeat resizeMode='cover' source={require('../assets/video/turntable.mp4')} style={styles.backgroundVideo}/>To enable audio to play in background on iOS the audio session needs to be set toAVAudioSessionCategoryPlayback. SeeApple documentation for additional details. (NOTE: there is now a ticket toexpose this as a prop )
seek(seconds)
Seeks the video to the specified time (in seconds). Access using a ref to the component
presentFullscreenPlayer()
Toggles a fullscreen player. Access using a ref to the component.
See anExample integration in
react-native-loginnote that this example uses an older version of this library, before we usedexport default-- if you userequireyou will need to dorequire('react-native-video').defaultas per instructions above.Try the includedVideoPlayer example yourself:
git clone git@github.com:react-native-community/react-native-video.gitcd react-native-video/examplenpm installopen ios/VideoPlayer.xcodeprojThen
Cmd+Rto start the React Packager, build and run the project in the simulator.Lumpen Radio contains another example integration using local files and full screen background video.
- Add support for captions
- Add support for playing multiple videos in a sequence (will interfere with current
repeatimplementation) - Callback to get buffering progress for remote videos
- Bring API closer to HTML5
<Video>reference
MIT Licensed
About
A <Video /> component for react-native
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Languages
- Java58.8%
- Objective-C21.4%
- JavaScript15.4%
- C#3.2%
- Other1.2%