【发布时间】:2017-09-20 02:06:07
【问题描述】:
我正在尝试在 react 上创建一个媒体播放器,我想使用 react 媒体事件,但我很困惑如何使用它们。需要一些帮助。
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;
上面是我的代码。
我正在尝试播放或暂停视频,但我不知道该怎么做。
使用 Javascript 我可以执行这些操作,但我想知道在 React 中应该如何完成。
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';
}
}
【问题讨论】:
标签: reactjs