2021年10月13日 星期三

[expo, expo-av, seek] How to play video using expo-av with play, pause, and seek functions

How to play video using expo-av with play, pause, and seek functions

井民全, Jing, mqjing@gmail.com

[hybrid, cross] Ionic, React Native, Cordova, Electron

GitHub: Source

1. Quick

1.1. Seup

expo init VideoPlayer   # create a new project

cd VideoPlayer

expo install expo-av @react-native-community/slider    # install dependencies


expo start


1.2. Key



(Edit)

1.3. Handle compile issue

1.3.1. Property 'isPlaying' does not exist on type '{}'

=> 

Change

From

const video = React.useRef(null);

const [status, setStatus] = React.useState({});


To

const video = React.useRef(null as any);

const [status, setStatus] = React.useState({} as any);




2. Detail

2.1. Create a new project from blank TypeScript template

expo init VideoPlayer

Choose blank (TypeScript)

2.2. Install

cd VideoPlayer

expo install expo-av @react-native-community/slider


2.3. Code (TypeScript)

File: App.tsx

import { StatusBar } from 'expo-status-bar';

import React, { useRef, useState } from 'react'

import { Dimensions, StyleSheet, Text, ScrollView, View, Button, Alert } from 'react-native';


import { Video } from 'expo-av'


const { width: DEVICE_WIDTH, height: DEVICE_HEIGHT } = Dimensions.get("window");



export default function App() {

  const video = React.useRef(null as any);

  const [status, setStatus] = React.useState({} as any);


  return (

      <View style={styles.container}>

      <Text style={[styles.text, { fontWeight: 'bold', textTransform: 'uppercase' }]}>

        Examples 1

      </Text>


      <Video

        ref={video}

        style={{ width: DEVICE_WIDTH, height: 300 }}

        source={{ uri: 'http://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4' }}

        shouldPlay

        useNativeControls

        resizeMode="contain"

        isLooping

        onPlaybackStatusUpdate={status => setStatus(() => status)}

      />


             {/* Control panel */}

      <View style={styles.viewControlButton}

      >

                  {/* Play button */}

            <Button

                  title={status.isPlaying ? 'Pause' : 'Play'}

                  onPress={() =>

                        status.isPlaying ? video.current.pauseAsync() : video.current.playAsync()

                  }

            />

                  {/* Seek button*/}

            <Button

                  title={'Seek'}

                  onPress={() => {

                        if (status.isPlaying) {

                                  video.current.setPositionAsync(2000);

                        } else {

                                  Alert.alert('Click [Play]');

                    }

                }

              } 

        />

      </View>


      <StatusBar style="auto" />

       </View>


  );

}


const styles = StyleSheet.create({

  container: {

    flex: 1,

    backgroundColor: '#fff',

    alignItems: 'center',

    justifyContent: 'center',

  },


  viewControlButton: {

    flex: 0,

    flexDirection: 'row',

    justifyContent: 'center',

    alignItems: 'center',

  },


  text: {

    marginTop: 36,

    marginBottom: 12,

  },

});



3. Run

expo start


Result


4. Reference

1. https://docs.expo.dev/versions/latest/sdk/video/#props