2019-08-24 05:38:02 +09:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import { formatTime } from 'mastodon/features/video';
|
|
|
|
import Icon from 'mastodon/components/icon';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { throttle } from 'lodash';
|
2020-06-23 19:20:14 +09:00
|
|
|
import { getPointerPosition, fileNameFromURL } from 'mastodon/features/video';
|
2020-06-24 17:25:32 +09:00
|
|
|
import { debounce } from 'lodash';
|
2020-06-21 09:27:19 +09:00
|
|
|
|
2020-07-06 01:28:25 +09:00
|
|
|
const hex2rgba = (hex, alpha = 1) => {
|
|
|
|
const [r, g, b] = hex.match(/\w\w/g).map(x => parseInt(x, 16));
|
|
|
|
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
2020-06-21 09:27:19 +09:00
|
|
|
};
|
2019-08-24 05:38:02 +09:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
play: { id: 'video.play', defaultMessage: 'Play' },
|
|
|
|
pause: { id: 'video.pause', defaultMessage: 'Pause' },
|
|
|
|
mute: { id: 'video.mute', defaultMessage: 'Mute sound' },
|
|
|
|
unmute: { id: 'video.unmute', defaultMessage: 'Unmute sound' },
|
2019-10-25 18:48:20 +09:00
|
|
|
download: { id: 'video.download', defaultMessage: 'Download file' },
|
2019-08-24 05:38:02 +09:00
|
|
|
});
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
const TICK_SIZE = 10;
|
2020-06-23 19:20:14 +09:00
|
|
|
const PADDING = 180;
|
2020-06-21 09:27:19 +09:00
|
|
|
|
2019-08-24 05:38:02 +09:00
|
|
|
export default @injectIntl
|
|
|
|
class Audio extends React.PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
src: PropTypes.string.isRequired,
|
|
|
|
alt: PropTypes.string,
|
2020-06-21 09:27:19 +09:00
|
|
|
poster: PropTypes.string,
|
2019-08-24 05:38:02 +09:00
|
|
|
duration: PropTypes.number,
|
2020-06-21 09:27:19 +09:00
|
|
|
width: PropTypes.number,
|
2019-08-24 05:38:02 +09:00
|
|
|
height: PropTypes.number,
|
|
|
|
editable: PropTypes.bool,
|
2020-06-25 08:33:01 +09:00
|
|
|
fullscreen: PropTypes.bool,
|
2019-08-24 05:38:02 +09:00
|
|
|
intl: PropTypes.object.isRequired,
|
2020-06-21 09:27:19 +09:00
|
|
|
cacheWidth: PropTypes.func,
|
2020-07-06 01:28:25 +09:00
|
|
|
backgroundColor: PropTypes.string,
|
|
|
|
foregroundColor: PropTypes.string,
|
|
|
|
accentColor: PropTypes.string,
|
2019-08-24 05:38:02 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
2020-06-21 09:27:19 +09:00
|
|
|
width: this.props.width,
|
2019-08-24 05:38:02 +09:00
|
|
|
currentTime: 0,
|
2020-06-21 09:27:19 +09:00
|
|
|
buffer: 0,
|
2019-08-24 05:38:02 +09:00
|
|
|
duration: null,
|
|
|
|
paused: true,
|
|
|
|
muted: false,
|
|
|
|
volume: 0.5,
|
2020-06-21 09:27:19 +09:00
|
|
|
dragging: false,
|
2019-08-24 05:38:02 +09:00
|
|
|
};
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
setPlayerRef = c => {
|
|
|
|
this.player = c;
|
|
|
|
|
2020-06-24 17:25:32 +09:00
|
|
|
if (this.player) {
|
|
|
|
this._setDimensions();
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 09:27:19 +09:00
|
|
|
|
2020-06-24 17:25:32 +09:00
|
|
|
_setDimensions () {
|
|
|
|
const width = this.player.offsetWidth;
|
2020-06-25 08:33:01 +09:00
|
|
|
const height = this.props.fullscreen ? this.player.offsetHeight : (width / (16/9));
|
2020-06-21 09:27:19 +09:00
|
|
|
|
2020-06-24 17:25:32 +09:00
|
|
|
if (this.props.cacheWidth) {
|
|
|
|
this.props.cacheWidth(width);
|
2020-06-21 09:27:19 +09:00
|
|
|
}
|
2020-06-24 17:25:32 +09:00
|
|
|
|
|
|
|
this.setState({ width, height });
|
2020-06-21 09:27:19 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
setSeekRef = c => {
|
|
|
|
this.seek = c;
|
|
|
|
}
|
|
|
|
|
2019-08-24 05:38:02 +09:00
|
|
|
setVolumeRef = c => {
|
|
|
|
this.volume = c;
|
|
|
|
}
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
setAudioRef = c => {
|
|
|
|
this.audio = c;
|
|
|
|
|
|
|
|
if (this.audio) {
|
|
|
|
this.setState({ volume: this.audio.volume, muted: this.audio.muted });
|
|
|
|
}
|
2019-08-24 05:38:02 +09:00
|
|
|
}
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
setCanvasRef = c => {
|
|
|
|
this.canvas = c;
|
|
|
|
|
|
|
|
if (c) {
|
|
|
|
this.canvasContext = c.getContext('2d');
|
2019-08-24 05:38:02 +09:00
|
|
|
}
|
2020-06-21 09:27:19 +09:00
|
|
|
}
|
2020-01-11 10:02:21 +09:00
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
componentDidMount () {
|
2020-01-11 10:02:21 +09:00
|
|
|
window.addEventListener('scroll', this.handleScroll);
|
2020-06-24 17:25:32 +09:00
|
|
|
window.addEventListener('resize', this.handleResize, { passive: true });
|
2019-08-24 05:38:02 +09:00
|
|
|
}
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
componentDidUpdate (prevProps, prevState) {
|
2020-07-06 01:28:25 +09:00
|
|
|
if (prevProps.src !== this.props.src || this.state.width !== prevState.width || this.state.height !== prevState.height) {
|
|
|
|
this._clear();
|
|
|
|
this._draw();
|
2020-06-21 09:27:19 +09:00
|
|
|
}
|
2020-06-29 20:56:55 +09:00
|
|
|
}
|
|
|
|
|
2019-08-24 05:38:02 +09:00
|
|
|
componentWillUnmount () {
|
2020-01-11 10:02:21 +09:00
|
|
|
window.removeEventListener('scroll', this.handleScroll);
|
2020-06-24 17:25:32 +09:00
|
|
|
window.removeEventListener('resize', this.handleResize);
|
2020-06-21 09:27:19 +09:00
|
|
|
}
|
2020-01-11 10:02:21 +09:00
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
togglePlay = () => {
|
|
|
|
if (this.state.paused) {
|
|
|
|
this.setState({ paused: false }, () => this.audio.play());
|
|
|
|
} else {
|
|
|
|
this.setState({ paused: true }, () => this.audio.pause());
|
2019-08-24 05:38:02 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 17:25:32 +09:00
|
|
|
handleResize = debounce(() => {
|
|
|
|
if (this.player) {
|
|
|
|
this._setDimensions();
|
|
|
|
}
|
|
|
|
}, 250, {
|
|
|
|
trailing: true,
|
|
|
|
});
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
handlePlay = () => {
|
|
|
|
this.setState({ paused: false });
|
2019-08-24 05:38:02 +09:00
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
if (this.canvas && !this.audioContext) {
|
|
|
|
this._initAudioContext();
|
|
|
|
}
|
2019-08-24 05:38:02 +09:00
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
if (this.audioContext && this.audioContext.state === 'suspended') {
|
|
|
|
this.audioContext.resume();
|
2019-08-24 05:38:02 +09:00
|
|
|
}
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
this._renderCanvas();
|
|
|
|
}
|
2019-08-24 05:38:02 +09:00
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
handlePause = () => {
|
|
|
|
this.setState({ paused: true });
|
2019-08-24 05:38:02 +09:00
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
if (this.audioContext) {
|
|
|
|
this.audioContext.suspend();
|
2019-08-24 05:38:02 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
handleProgress = () => {
|
2020-06-25 08:33:01 +09:00
|
|
|
const lastTimeRange = this.audio.buffered.length - 1;
|
|
|
|
|
|
|
|
if (lastTimeRange > -1) {
|
|
|
|
this.setState({ buffer: Math.ceil(this.audio.buffered.end(lastTimeRange) / this.audio.duration * 100) });
|
2019-08-24 05:38:02 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleMute = () => {
|
2020-01-11 10:02:21 +09:00
|
|
|
const muted = !this.state.muted;
|
2020-06-21 09:27:19 +09:00
|
|
|
|
|
|
|
this.setState({ muted }, () => {
|
|
|
|
this.audio.muted = muted;
|
|
|
|
});
|
2019-08-24 05:38:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
handleVolumeMouseDown = e => {
|
|
|
|
document.addEventListener('mousemove', this.handleMouseVolSlide, true);
|
|
|
|
document.addEventListener('mouseup', this.handleVolumeMouseUp, true);
|
|
|
|
document.addEventListener('touchmove', this.handleMouseVolSlide, true);
|
|
|
|
document.addEventListener('touchend', this.handleVolumeMouseUp, true);
|
|
|
|
|
|
|
|
this.handleMouseVolSlide(e);
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleVolumeMouseUp = () => {
|
|
|
|
document.removeEventListener('mousemove', this.handleMouseVolSlide, true);
|
|
|
|
document.removeEventListener('mouseup', this.handleVolumeMouseUp, true);
|
|
|
|
document.removeEventListener('touchmove', this.handleMouseVolSlide, true);
|
|
|
|
document.removeEventListener('touchend', this.handleVolumeMouseUp, true);
|
|
|
|
}
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
handleMouseDown = e => {
|
|
|
|
document.addEventListener('mousemove', this.handleMouseMove, true);
|
|
|
|
document.addEventListener('mouseup', this.handleMouseUp, true);
|
|
|
|
document.addEventListener('touchmove', this.handleMouseMove, true);
|
|
|
|
document.addEventListener('touchend', this.handleMouseUp, true);
|
|
|
|
|
|
|
|
this.setState({ dragging: true });
|
|
|
|
this.audio.pause();
|
|
|
|
this.handleMouseMove(e);
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseUp = () => {
|
|
|
|
document.removeEventListener('mousemove', this.handleMouseMove, true);
|
|
|
|
document.removeEventListener('mouseup', this.handleMouseUp, true);
|
|
|
|
document.removeEventListener('touchmove', this.handleMouseMove, true);
|
|
|
|
document.removeEventListener('touchend', this.handleMouseUp, true);
|
|
|
|
|
|
|
|
this.setState({ dragging: false });
|
|
|
|
this.audio.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseMove = throttle(e => {
|
|
|
|
const { x } = getPointerPosition(this.seek, e);
|
2020-06-25 08:33:01 +09:00
|
|
|
const currentTime = this.audio.duration * x;
|
2020-06-21 09:27:19 +09:00
|
|
|
|
|
|
|
if (!isNaN(currentTime)) {
|
|
|
|
this.setState({ currentTime }, () => {
|
|
|
|
this.audio.currentTime = currentTime;
|
|
|
|
});
|
|
|
|
}
|
2020-06-25 08:33:01 +09:00
|
|
|
}, 15);
|
2020-06-21 09:27:19 +09:00
|
|
|
|
|
|
|
handleTimeUpdate = () => {
|
|
|
|
this.setState({
|
2020-06-25 08:33:01 +09:00
|
|
|
currentTime: this.audio.currentTime,
|
2020-06-21 09:27:19 +09:00
|
|
|
duration: Math.floor(this.audio.duration),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-24 05:38:02 +09:00
|
|
|
handleMouseVolSlide = throttle(e => {
|
2020-06-23 19:20:14 +09:00
|
|
|
const { x } = getPointerPosition(this.volume, e);
|
2019-08-24 05:38:02 +09:00
|
|
|
|
|
|
|
if(!isNaN(x)) {
|
2020-06-23 19:20:14 +09:00
|
|
|
this.setState({ volume: x }, () => {
|
|
|
|
this.audio.volume = x;
|
2020-06-21 09:27:19 +09:00
|
|
|
});
|
2019-08-24 05:38:02 +09:00
|
|
|
}
|
2020-06-25 08:33:01 +09:00
|
|
|
}, 15);
|
2019-08-24 05:38:02 +09:00
|
|
|
|
2020-01-11 10:02:21 +09:00
|
|
|
handleScroll = throttle(() => {
|
2020-06-21 09:27:19 +09:00
|
|
|
if (!this.canvas || !this.audio) {
|
2020-01-11 10:02:21 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
const { top, height } = this.canvas.getBoundingClientRect();
|
2020-01-11 10:02:21 +09:00
|
|
|
const inView = (top <= (window.innerHeight || document.documentElement.clientHeight)) && (top + height >= 0);
|
|
|
|
|
|
|
|
if (!this.state.paused && !inView) {
|
2020-06-21 09:27:19 +09:00
|
|
|
this.setState({ paused: true }, () => this.audio.pause());
|
2020-01-11 10:02:21 +09:00
|
|
|
}
|
2020-06-21 09:27:19 +09:00
|
|
|
}, 150, { trailing: true });
|
|
|
|
|
2020-06-23 19:20:14 +09:00
|
|
|
handleMouseEnter = () => {
|
|
|
|
this.setState({ hovered: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseLeave = () => {
|
|
|
|
this.setState({ hovered: false });
|
|
|
|
}
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
_initAudioContext () {
|
|
|
|
const context = new AudioContext();
|
|
|
|
const analyser = context.createAnalyser();
|
|
|
|
const source = context.createMediaElementSource(this.audio);
|
|
|
|
|
|
|
|
analyser.smoothingTimeConstant = 0.6;
|
|
|
|
analyser.fftSize = 2048;
|
|
|
|
|
|
|
|
source.connect(analyser);
|
|
|
|
source.connect(context.destination);
|
|
|
|
|
|
|
|
this.audioContext = context;
|
|
|
|
this.analyser = analyser;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:20:14 +09:00
|
|
|
handleDownload = () => {
|
|
|
|
fetch(this.props.src).then(res => res.blob()).then(blob => {
|
|
|
|
const element = document.createElement('a');
|
|
|
|
const objectURL = URL.createObjectURL(blob);
|
|
|
|
|
|
|
|
element.setAttribute('href', objectURL);
|
|
|
|
element.setAttribute('download', fileNameFromURL(this.props.src));
|
|
|
|
|
|
|
|
document.body.appendChild(element);
|
|
|
|
element.click();
|
|
|
|
document.body.removeChild(element);
|
|
|
|
|
|
|
|
URL.revokeObjectURL(objectURL);
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
_renderCanvas () {
|
|
|
|
requestAnimationFrame(() => {
|
2020-06-25 08:33:01 +09:00
|
|
|
this.handleTimeUpdate();
|
2020-06-21 09:27:19 +09:00
|
|
|
this._clear();
|
|
|
|
this._draw();
|
|
|
|
|
|
|
|
if (!this.state.paused) {
|
|
|
|
this._renderCanvas();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_clear () {
|
|
|
|
this.canvasContext.clearRect(0, 0, this.state.width, this.state.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
_draw () {
|
|
|
|
this.canvasContext.save();
|
|
|
|
|
|
|
|
const ticks = this._getTicks(360 * this._getScaleCoefficient(), TICK_SIZE);
|
|
|
|
|
|
|
|
ticks.forEach(tick => {
|
|
|
|
this._drawTick(tick.x1, tick.y1, tick.x2, tick.y2);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.canvasContext.restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getRadius () {
|
|
|
|
return parseInt(((this.state.height || this.props.height) - (PADDING * this._getScaleCoefficient()) * 2) / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getScaleCoefficient () {
|
|
|
|
return (this.state.height || this.props.height) / 982;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getTicks (count, size, animationParams = [0, 90]) {
|
|
|
|
const radius = this._getRadius();
|
|
|
|
const ticks = this._getTickPoints(count);
|
|
|
|
const lesser = 200;
|
|
|
|
const m = [];
|
|
|
|
const bufferLength = this.analyser ? this.analyser.frequencyBinCount : 0;
|
|
|
|
const frequencyData = new Uint8Array(bufferLength);
|
|
|
|
const allScales = [];
|
|
|
|
const scaleCoefficient = this._getScaleCoefficient();
|
|
|
|
|
|
|
|
if (this.analyser) {
|
|
|
|
this.analyser.getByteFrequencyData(frequencyData);
|
|
|
|
}
|
|
|
|
|
|
|
|
ticks.forEach((tick, i) => {
|
|
|
|
const coef = 1 - i / (ticks.length * 2.5);
|
|
|
|
|
|
|
|
let delta = ((frequencyData[i] || 0) - lesser * coef) * scaleCoefficient;
|
|
|
|
|
|
|
|
if (delta < 0) {
|
|
|
|
delta = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let k;
|
|
|
|
|
|
|
|
if (animationParams[0] <= tick.angle && tick.angle <= animationParams[1]) {
|
|
|
|
k = radius / (radius - this._getSize(tick.angle, animationParams[0], animationParams[1]) - delta);
|
|
|
|
} else {
|
|
|
|
k = radius / (radius - (size + delta));
|
|
|
|
}
|
|
|
|
|
|
|
|
const x1 = tick.x * (radius - size);
|
|
|
|
const y1 = tick.y * (radius - size);
|
|
|
|
const x2 = x1 * k;
|
|
|
|
const y2 = y1 * k;
|
|
|
|
|
|
|
|
m.push({ x1, y1, x2, y2 });
|
|
|
|
|
|
|
|
if (i < 20) {
|
|
|
|
let scale = delta / (200 * scaleCoefficient);
|
|
|
|
scale = scale < 1 ? 1 : scale;
|
|
|
|
allScales.push(scale);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const scale = allScales.reduce((pv, cv) => pv + cv, 0) / allScales.length;
|
|
|
|
|
|
|
|
return m.map(({ x1, y1, x2, y2 }) => ({
|
|
|
|
x1: x1,
|
|
|
|
y1: y1,
|
|
|
|
x2: x2 * scale,
|
|
|
|
y2: y2 * scale,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
_getSize (angle, l, r) {
|
|
|
|
const scaleCoefficient = this._getScaleCoefficient();
|
|
|
|
const maxTickSize = TICK_SIZE * 9 * scaleCoefficient;
|
|
|
|
const m = (r - l) / 2;
|
|
|
|
const x = (angle - l);
|
|
|
|
|
|
|
|
let h;
|
|
|
|
|
|
|
|
if (x === m) {
|
|
|
|
return maxTickSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
const d = Math.abs(m - x);
|
|
|
|
const v = 40 * Math.sqrt(1 / d);
|
|
|
|
|
|
|
|
if (v > maxTickSize) {
|
|
|
|
h = maxTickSize;
|
|
|
|
} else {
|
|
|
|
h = Math.max(TICK_SIZE, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getTickPoints (count) {
|
|
|
|
const PI = 360;
|
|
|
|
const coords = [];
|
|
|
|
const step = PI / count;
|
|
|
|
|
|
|
|
let rad;
|
|
|
|
|
|
|
|
for(let deg = 0; deg < PI; deg += step) {
|
|
|
|
rad = deg * Math.PI / (PI / 2);
|
|
|
|
coords.push({ x: Math.cos(rad), y: -Math.sin(rad), angle: deg });
|
|
|
|
}
|
|
|
|
|
|
|
|
return coords;
|
|
|
|
}
|
|
|
|
|
|
|
|
_drawTick (x1, y1, x2, y2) {
|
2020-06-24 17:25:32 +09:00
|
|
|
const cx = this._getCX();
|
|
|
|
const cy = this._getCY();
|
2020-06-21 09:27:19 +09:00
|
|
|
|
2020-06-24 17:25:32 +09:00
|
|
|
const dx1 = Math.ceil(cx + x1);
|
|
|
|
const dy1 = Math.ceil(cy + y1);
|
|
|
|
const dx2 = Math.ceil(cx + x2);
|
|
|
|
const dy2 = Math.ceil(cy + y2);
|
2020-06-21 09:27:19 +09:00
|
|
|
|
|
|
|
const gradient = this.canvasContext.createLinearGradient(dx1, dy1, dx2, dy2);
|
|
|
|
|
2020-07-06 01:28:25 +09:00
|
|
|
const mainColor = this._getAccentColor();
|
|
|
|
const lastColor = hex2rgba(mainColor, 0);
|
2020-06-21 09:27:19 +09:00
|
|
|
|
|
|
|
gradient.addColorStop(0, mainColor);
|
|
|
|
gradient.addColorStop(0.6, mainColor);
|
|
|
|
gradient.addColorStop(1, lastColor);
|
|
|
|
|
|
|
|
this.canvasContext.beginPath();
|
|
|
|
this.canvasContext.strokeStyle = gradient;
|
|
|
|
this.canvasContext.lineWidth = 2;
|
|
|
|
this.canvasContext.moveTo(dx1, dy1);
|
|
|
|
this.canvasContext.lineTo(dx2, dy2);
|
|
|
|
this.canvasContext.stroke();
|
|
|
|
}
|
|
|
|
|
2020-06-24 17:25:32 +09:00
|
|
|
_getCX() {
|
|
|
|
return Math.floor(this.state.width / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getCY() {
|
|
|
|
return Math.floor(this._getRadius() + (PADDING * this._getScaleCoefficient()));
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:28:25 +09:00
|
|
|
_getAccentColor () {
|
|
|
|
return this.props.accentColor || '#ffffff';
|
|
|
|
}
|
|
|
|
|
|
|
|
_getBackgroundColor () {
|
|
|
|
return this.props.backgroundColor || '#000000';
|
|
|
|
}
|
|
|
|
|
|
|
|
_getForegroundColor () {
|
|
|
|
return this.props.foregroundColor || '#ffffff';
|
2020-06-21 09:27:19 +09:00
|
|
|
}
|
2020-01-11 10:02:21 +09:00
|
|
|
|
2019-08-24 05:38:02 +09:00
|
|
|
render () {
|
2020-06-21 09:27:19 +09:00
|
|
|
const { src, intl, alt, editable } = this.props;
|
2020-07-06 01:28:25 +09:00
|
|
|
const { paused, muted, volume, currentTime, duration, buffer, dragging } = this.state;
|
2020-06-23 19:20:14 +09:00
|
|
|
const progress = (currentTime / duration) * 100;
|
2019-08-24 05:38:02 +09:00
|
|
|
|
|
|
|
return (
|
2020-07-06 01:28:25 +09:00
|
|
|
<div className={classNames('audio-player', { editable })} ref={this.setPlayerRef} style={{ backgroundColor: this._getBackgroundColor(), color: this._getForegroundColor(), width: '100%', height: this.props.fullscreen ? '100%' : (this.state.height || this.props.height) }} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
|
2020-06-21 09:27:19 +09:00
|
|
|
<audio
|
|
|
|
src={src}
|
|
|
|
ref={this.setAudioRef}
|
|
|
|
preload='none'
|
|
|
|
onPlay={this.handlePlay}
|
|
|
|
onPause={this.handlePause}
|
|
|
|
onProgress={this.handleProgress}
|
2020-06-23 02:24:16 +09:00
|
|
|
crossOrigin='anonymous'
|
2020-06-21 09:27:19 +09:00
|
|
|
/>
|
2019-08-24 05:38:02 +09:00
|
|
|
|
2020-06-21 09:27:19 +09:00
|
|
|
<canvas
|
|
|
|
role='button'
|
|
|
|
className='audio-player__canvas'
|
|
|
|
width={this.state.width}
|
|
|
|
height={this.state.height}
|
2020-07-06 01:28:25 +09:00
|
|
|
style={{ width: '100%', position: 'absolute', top: 0, left: 0 }}
|
2020-06-21 09:27:19 +09:00
|
|
|
ref={this.setCanvasRef}
|
2020-07-06 01:28:25 +09:00
|
|
|
onClick={this.togglePlay}
|
|
|
|
title={alt}
|
|
|
|
aria-label={alt}
|
2020-06-21 09:27:19 +09:00
|
|
|
/>
|
|
|
|
|
|
|
|
<img
|
|
|
|
src={this.props.poster}
|
|
|
|
alt=''
|
|
|
|
width={(this._getRadius() - TICK_SIZE) * 2}
|
|
|
|
height={(this._getRadius() - TICK_SIZE) * 2}
|
2020-06-24 17:25:32 +09:00
|
|
|
style={{ position: 'absolute', left: this._getCX(), top: this._getCY(), transform: 'translate(-50%, -50%)', borderRadius: '50%', pointerEvents: 'none' }}
|
2020-06-21 09:27:19 +09:00
|
|
|
/>
|
|
|
|
|
|
|
|
<div className='video-player__seek' onMouseDown={this.handleMouseDown} ref={this.setSeekRef}>
|
|
|
|
<div className='video-player__seek__buffer' style={{ width: `${buffer}%` }} />
|
2020-07-06 01:28:25 +09:00
|
|
|
<div className='video-player__seek__progress' style={{ width: `${progress}%`, backgroundColor: this._getAccentColor() }} />
|
2020-06-21 09:27:19 +09:00
|
|
|
|
|
|
|
<span
|
|
|
|
className={classNames('video-player__seek__handle', { active: dragging })}
|
|
|
|
tabIndex='0'
|
2020-07-06 01:28:25 +09:00
|
|
|
style={{ left: `${progress}%`, backgroundColor: this._getAccentColor() }}
|
2020-06-21 09:27:19 +09:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2019-08-24 05:38:02 +09:00
|
|
|
<div className='video-player__controls active'>
|
|
|
|
<div className='video-player__buttons-bar'>
|
|
|
|
<div className='video-player__buttons left'>
|
2020-03-05 23:52:15 +09:00
|
|
|
<button type='button' title={intl.formatMessage(paused ? messages.play : messages.pause)} aria-label={intl.formatMessage(paused ? messages.play : messages.pause)} onClick={this.togglePlay}><Icon id={paused ? 'play' : 'pause'} fixedWidth /></button>
|
|
|
|
<button type='button' title={intl.formatMessage(muted ? messages.unmute : messages.mute)} aria-label={intl.formatMessage(muted ? messages.unmute : messages.mute)} onClick={this.toggleMute}><Icon id={muted ? 'volume-off' : 'volume-up'} fixedWidth /></button>
|
2019-08-24 05:38:02 +09:00
|
|
|
|
2020-06-23 19:20:14 +09:00
|
|
|
<div className={classNames('video-player__volume', { active: this.state.hovered })} ref={this.setVolumeRef} onMouseDown={this.handleVolumeMouseDown}>
|
2020-07-06 01:28:25 +09:00
|
|
|
<div className='video-player__volume__current' style={{ width: `${volume * 100}%`, backgroundColor: this._getAccentColor() }} />
|
2019-08-24 05:38:02 +09:00
|
|
|
|
|
|
|
<span
|
|
|
|
className={classNames('video-player__volume__handle')}
|
|
|
|
tabIndex='0'
|
2020-07-06 01:28:25 +09:00
|
|
|
style={{ left: `${volume * 100}%`, backgroundColor: this._getAccentColor() }}
|
2019-08-24 05:38:02 +09:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2020-06-23 19:20:14 +09:00
|
|
|
<span className='video-player__time'>
|
2020-06-25 08:33:01 +09:00
|
|
|
<span className='video-player__time-current'>{formatTime(Math.floor(currentTime))}</span>
|
2019-08-24 05:38:02 +09:00
|
|
|
<span className='video-player__time-sep'>/</span>
|
|
|
|
<span className='video-player__time-total'>{formatTime(this.state.duration || Math.floor(this.props.duration))}</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
2019-10-25 18:48:20 +09:00
|
|
|
|
|
|
|
<div className='video-player__buttons right'>
|
2020-06-23 19:20:14 +09:00
|
|
|
<button type='button' title={intl.formatMessage(messages.download)} aria-label={intl.formatMessage(messages.download)} onClick={this.handleDownload}><Icon id='download' fixedWidth /></button>
|
2019-10-25 18:48:20 +09:00
|
|
|
</div>
|
2019-08-24 05:38:02 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|