1

I am trying to create a media player on react and I want to use react media events but I am confused how to use them. Need some help with it.

import React, { Component } from 'react';

class Player extends Component {
  constructor(props) {
    super(props);

  }

  playOrPause() {
    #how should I check here if video is playing or not
  }

  render() {
    return (
      <div id="player">
          <div id="video-container">
            <video key={this.props.video.song} poster={this.props.video.cover_image} controls>
              <source src={this.props.video.url} type="audio/mpeg" />
            </video>
          </div>
          <div id="pbar-container">
            <div id="pbar"></div>
          </div>
          <div id="buttons-container">
            <img id="play-button" onClick={this.playOrPause} src="images/play.png" alt="play"/>
            <div id="time-field">
             0:00 / 0:00
            </div>
            <img id="sound-button" src="images/sound.png" alt="sound"/>
            <div id="sbar-container">
              <div id="sbar"></div>
            </div>
            <img id="fullscreen-button" src="images/fullscreen.png" alt="fullscreen"/>
          </div>
        </div>
    );
  }
}

export default Player;

Above is my code.

I am trying to play or pause video, but I don't know how to do it.

Using Javascript I can perform these actions but I want to know how in React it should be done.

window.addEventListener('load', function() {

    video = document.getElementById('video');

    playButton = document.getElementById('play-button');

    video.load();
    video.addEventListener('canplay', function() {

        playButton.addEventListener('click', playOrPause, false);

    }, false);

}, false);

function playOrPause() {
    if (video.paused) {
        video.play();
        playButton.src = 'images/pause.png';
    } else {
        video.pause();
        playButton.src = 'images/play.png';
    }
}
Rahul Shrivastava
  • 1,391
  • 3
  • 14
  • 38
  • Have you tried this solution http://stackoverflow.com/questions/6877403/how-to-tell-if-a-video-element-is-currently-playing – elmeister Apr 23 '17 at 11:42

1 Answers1

1

You can use ref functions to obtain the actual HTML video element.

I've simplified it below to just demonstrate how to use a ref to obtain the video element, allowing you to load/play/pause as you would with pure JS.

class Player extends Component { 
  state = {
    playing: false
  }

  componentDidMount() {
    this.video.load()
  }

  playOrPause = () => {
    // this.video is the element and can be used inside other methods
    if (this.video.paused) {
      this.video.play()
      this.setState(() => ({ playing: true }))
    } else {
      video.pause()
      this.setState(() => ({ playing: false }))
    }
  }

  render() {
    // El here is the HTML video element
    const ref = (el) => { this.video = el }
    const { playing } = this.state;
    return (
      <div>
        <video ref={ref} />
        <button onClick={this.playOrPause}>
          <img 
            src={playing ? 'images/pause.png' : 'images/play.png'} 
            alt={playing ? 'pause' : 'play'} 
          />
        </button>
      </div>
    )
  }
}
Alex Faunt
  • 529
  • 4
  • 6
  • video.play() returns a promise though (it throws if you try to play without user interaction first), you should wait for that first. – Ali Mert Çakar Jan 13 '22 at 12:30