0.2.23 • Published 2 years ago

react-native-native-video v0.2.23

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-native-native-video

Native Video Access for React Native

Working for iOS and Android!

Android support is second class, however, unless I finally have more time (or more money lol).

Installation

npm install react-native-native-video

Usage

Call openVideo(uri: string) to get a NativeVideoWrapper.

Get frames by various methods.

NativeVideoFrameView takes a NativeFrameWrapper in order to display.

import * as React from 'react';
import * as ImagePicker from 'expo-image-picker';
import { StyleSheet, View, Text, Button, Alert, TextInput } from 'react-native';
import { NativeVideoFrameView, NativeVideoWrapper, NativeFrameWrapper, openVideo } from "react-native-native-video";
export default function App() {
  const [pickedUri, setPickedUri] = React.useState<string>();
  const [frame, setFrame] = React.useState<NativeFrameWrapper>();
  const vidRef = React.useRef<NativeVideoWrapper>();
  const onPickVideo = React.useCallback(async () => {
    await ImagePicker.getMediaLibraryPermissionsAsync();
    const res = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ImagePicker.MediaTypeOptions.Videos });
    if (res.cancelled) return;
    const uri = res.uri;
    setPickedUri(uri);
  });
  const onVideoProperties = React.useCallback(async () => {
    if (!pickedUri) return;
    const video = vidRef.current;
    if (!vidRef.current || vidRef.current.sourceUri != pickedUri) {
      video = openVideo(pickedUri);
      vidRef.current = video;
    }
    video = video!; // Make typescript not complain.
    Alert.alert(`Opened video`, `Video props are duration : ${video.duration}, frameRate: ${video.frameRate}, numFrames ${video.numFrames}, size ${video.size}`);
    setNumFrames(video.numFrames);
  });

  const onVideoFrameTest = React.useCallback(async () => {
    if (!pickedUri) return;
    let video = vidRef.current;
    if (!vidRef.current || vidRef.current.sourceUri != pickedUri) {
      video = openVideo(pickedUri);
      vidRef.current = video;
    }
    video = video!; // Make typescript not complain.
    const frameIdx = 0;
    const frame = video.getFrameAtIndex(frameIdx);
    Alert.alert(`Video frame ${frameIdx} isValid ${frame.isValid} frameSize ${JSON.stringify(frame.size)}`);
    // const arrayBuffer = frame.arrayBuffer;
    // console.log('arrayBuffer status', arrayBuffer);
    setFrame(frame);
  });

  const recentAnimFrame = React.useRef<ReturnType<typeof requestAnimationFrame>>();
  const safeSetViewFrameIndex = React.useCallback((idx: number) => {
    if (recentAnimFrame.current) {
      cancelAnimationFrame(recentAnimFrame.current);
    }
    const me = requestAnimationFrame(() => {
      const vid = vidRef.current;
      if (!vid) return;
      const frame = vid.getFrameAtIndex(Math.floor(idx));
      setFrame(frame);
      if (recentAnimFrame.current == me) {
        recentAnimFrame.current = undefined;
      }
    });
    recentAnimFrame.current = me;
  }, []);

  return (
    <View style={styles.container}>
      <Button title='Pick Video' onPress={onPickVideo} />
      <Slider
        minimumValue={0}
        maximumValue={numFrames - 1}
        onValueChange={(v) => {
          const vid = vidRef.current;
          if (!vid) return;
          safeSetViewFrameIndex(Math.floor(v));
        }}
      />
      <Text>Video uri: {pickedUri}</Text>
      <Button title='Get video properties' onPress={onVideoProperties} />
      <Button title='Test Frame getting' onPress={onVideoFrameTest} />
      <NativeVideoFrameView
        style={{ backgroundColor: 'blue', borderWidth: 1, borderRadius: 8, flex: 1, alignSelf: 'stretch' }}
        frameData={frame}
      />
    </View>
  );
}

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Tip Jar

This thing took a pretty long time to write, and it'll take some chunk of time to maintain too. If you appreciate my work, help buy me some soda 🥤 via the following routes.

Stellar Lumens (XLM) : 
GCVKPZQUDXWVNPIIMF3FXR6KWAOHTEWPZZM2AQE4J3TXR6ZDHXQHP5BQ
Cardano (ADA) : 
addr1q9datt8urnyuc2059tquh59sva0pja7jqg4nfhnje7xcy6zpndeesglqkxhjvcgdu820flcecjzunwp6qen4yr92gm6smssug8
0.2.23

2 years ago

0.2.22

2 years ago

0.2.21

2 years ago

0.2.20

2 years ago

0.2.19

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago