How to create a react video player with API response

Viewed 1007

Here is the code for React video player, use local storage video to create react player

Videoplayer :

import React from 'react';
import VideoPlayer from 'react-video-js-player';
import ban1 from './video/ban1.mp4'

const VideoJS = () => {
    const videoSrc = ban1;
    const poster = "https://unsplash.com/photos/ZRns2R5azu0"

    return (
        <div className="App">
            <h1> Fraction Video player</h1>
            <center><VideoPlayer src={videoSrc} poster={poster} width="720" /></center>

        </div>
    );
};
export default VideoJS;

After that I create some form and post date, sending requests to API, and Getting responses from API like this,  

["/home/vinsent/Videos/Fraction_webapp/FastAPI-RedisDB/videos/video7.mp4", "/home/vinsent/Videos/Fraction_webapp/FastAPI-RedisDB/videos/video19.mp4"]

Now how to pass this to react video player as Dynamic URL's

API CALL CODE :

import React, { Component } from 'react'
import axios from 'axios'

class PostForm extends Component {
    constructor(props) {
        super(props)

        this.state = {
            key: '',
            // Where data will be saved.
            data: [],
        }
        console.log(this.state)
    }

    changeHandler = e => {
        this.setState({ [e.target.name]: e.target.value })
    }

    submitHandler = e => {
        e.preventDefault()
        
        axios
        .get(`http://127.0.0.1:8000/hvals_hash?key=${this.state.key}`)
        .then(response => {
                        // Updating the state to trigger a re-render       
            this.setState({data: response.data});
            console.log(response.data)
        })
        .catch(error => {
            console.log(error)
        })
    }

    render() {
        const { key } = this.state
        
        return (
            <center><div>
                <form onSubmit={this.submitHandler}>
                    <div>
                        <h2> DATE PICKER</h2><br></br>
                        <input
                            type="text"
                            name="key"
                            value={key}
                            onChange={this.changeHandler}
                        />
                        
                    </div>
                    <br></br>
                    <button type="submit">Submit</button>
                </form>
            <div>
{this.state.data.map((videoURL) => {
    return (
    <div>
        <br></br>
        {
            <table>
            <ol>
                
                {videoURL}
            </ol>
            </table>
        }
    </div>
    );
})}

</div>
    
            </div></center>
        )
    }
}
export default PostForm

Expected answers :

  1. How to pass dynamic URL to the video player

  2. How to pass local video path to react video player

1 Answers

You can use react-player package for video or media purposes with the way which you want. Also It supports dynamic URL.

If you store your video path in a state, not const; you can handle the dynamic URL easily.

const [videoUrl, setVideoUrl] = useState('insertvideourlhere')
Related