2019-08-23 20:38:02 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2022-08-13 13:39:05 +00:00
|
|
|
import { defineMessages, FormattedMessage, injectIntl } from 'react-intl';
|
2023-02-13 19:09:01 +00:00
|
|
|
import { formatTime, getPointerPosition, fileNameFromURL } from 'flavours/glitch/features/video';
|
2019-08-23 20:38:02 +00:00
|
|
|
import Icon from 'flavours/glitch/components/icon';
|
|
|
|
import classNames from 'classnames';
|
2023-02-13 19:09:01 +00:00
|
|
|
import { throttle, debounce } from 'lodash';
|
2020-07-10 11:57:05 +00:00
|
|
|
import Visualizer from './visualizer';
|
2022-10-11 08:17:04 +00:00
|
|
|
import { displayMedia, useBlurhash } from 'flavours/glitch/initial_state';
|
2022-08-13 13:39:05 +00:00
|
|
|
import Blurhash from 'flavours/glitch/components/blurhash';
|
|
|
|
import { is } from 'immutable';
|
2019-08-23 20:38:02 +00: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 09:48:20 +00:00
|
|
|
download: { id: 'video.download', defaultMessage: 'Download file' },
|
2022-08-13 13:39:05 +00:00
|
|
|
hide: { id: 'audio.hide', defaultMessage: 'Hide audio' },
|
2019-08-23 20:38:02 +00:00
|
|
|
});
|
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
const TICK_SIZE = 10;
|
2020-06-23 10:20:14 +00:00
|
|
|
const PADDING = 180;
|
2020-06-21 00:27:19 +00:00
|
|
|
|
2019-08-23 20:38:02 +00:00
|
|
|
class Audio extends React.PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
src: PropTypes.string.isRequired,
|
|
|
|
alt: PropTypes.string,
|
2023-02-26 19:13:27 +00:00
|
|
|
lang: PropTypes.string,
|
2020-06-21 00:27:19 +00:00
|
|
|
poster: PropTypes.string,
|
2019-08-23 20:38:02 +00:00
|
|
|
duration: PropTypes.number,
|
2020-06-21 00:27:19 +00:00
|
|
|
width: PropTypes.number,
|
2019-08-23 20:38:02 +00:00
|
|
|
height: PropTypes.number,
|
2022-08-13 13:39:05 +00:00
|
|
|
sensitive: PropTypes.bool,
|
2019-08-23 20:38:02 +00:00
|
|
|
editable: PropTypes.bool,
|
2020-06-24 23:33:01 +00:00
|
|
|
fullscreen: PropTypes.bool,
|
2019-08-23 20:38:02 +00:00
|
|
|
intl: PropTypes.object.isRequired,
|
2022-08-13 13:39:05 +00:00
|
|
|
blurhash: PropTypes.string,
|
2020-06-21 00:27:19 +00:00
|
|
|
cacheWidth: PropTypes.func,
|
2022-08-13 13:39:05 +00:00
|
|
|
visible: PropTypes.bool,
|
|
|
|
onToggleVisibility: PropTypes.func,
|
2020-07-05 16:28:25 +00:00
|
|
|
backgroundColor: PropTypes.string,
|
|
|
|
foregroundColor: PropTypes.string,
|
|
|
|
accentColor: PropTypes.string,
|
2020-09-28 11:29:43 +00:00
|
|
|
currentTime: PropTypes.number,
|
2020-07-10 20:09:28 +00:00
|
|
|
autoPlay: PropTypes.bool,
|
2020-09-28 11:29:43 +00:00
|
|
|
volume: PropTypes.number,
|
|
|
|
muted: PropTypes.bool,
|
|
|
|
deployPictureInPicture: PropTypes.func,
|
2019-08-23 20:38:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
2020-06-21 00:27:19 +00:00
|
|
|
width: this.props.width,
|
2019-08-23 20:38:02 +00:00
|
|
|
currentTime: 0,
|
2020-06-21 00:27:19 +00:00
|
|
|
buffer: 0,
|
2019-08-23 20:38:02 +00:00
|
|
|
duration: null,
|
|
|
|
paused: true,
|
|
|
|
muted: false,
|
2023-01-24 15:24:46 +00:00
|
|
|
volume: 1,
|
2020-06-21 00:27:19 +00:00
|
|
|
dragging: false,
|
2022-08-13 13:39:05 +00:00
|
|
|
revealed: this.props.visible !== undefined ? this.props.visible : (displayMedia !== 'hide_all' && !this.props.sensitive || displayMedia === 'show_all'),
|
2019-08-23 20:38:02 +00:00
|
|
|
};
|
|
|
|
|
2020-07-10 11:57:05 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.visualizer = new Visualizer(TICK_SIZE);
|
|
|
|
}
|
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
setPlayerRef = c => {
|
|
|
|
this.player = c;
|
|
|
|
|
2020-06-24 08:25:32 +00:00
|
|
|
if (this.player) {
|
|
|
|
this._setDimensions();
|
|
|
|
}
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-06-21 00:27:19 +00:00
|
|
|
|
2020-09-28 11:29:43 +00:00
|
|
|
_pack() {
|
|
|
|
return {
|
|
|
|
src: this.props.src,
|
2023-01-24 15:24:46 +00:00
|
|
|
volume: this.state.volume,
|
|
|
|
muted: this.state.muted,
|
2020-09-28 11:29:43 +00:00
|
|
|
currentTime: this.audio.currentTime,
|
|
|
|
poster: this.props.poster,
|
|
|
|
backgroundColor: this.props.backgroundColor,
|
|
|
|
foregroundColor: this.props.foregroundColor,
|
|
|
|
accentColor: this.props.accentColor,
|
2022-08-13 13:39:05 +00:00
|
|
|
sensitive: this.props.sensitive,
|
|
|
|
visible: this.props.visible,
|
2020-09-28 11:29:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:25:32 +00:00
|
|
|
_setDimensions () {
|
|
|
|
const width = this.player.offsetWidth;
|
2020-06-24 23:33:01 +00:00
|
|
|
const height = this.props.fullscreen ? this.player.offsetHeight : (width / (16/9));
|
2020-06-21 00:27:19 +00:00
|
|
|
|
2020-08-11 19:01:37 +00:00
|
|
|
if (width && width != this.state.containerWidth) {
|
|
|
|
if (this.props.cacheWidth) {
|
|
|
|
this.props.cacheWidth(width);
|
|
|
|
}
|
2020-06-24 08:25:32 +00:00
|
|
|
|
2020-08-11 19:01:37 +00:00
|
|
|
this.setState({ width, height });
|
|
|
|
}
|
2020-06-21 00:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setSeekRef = c => {
|
|
|
|
this.seek = c;
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-06-21 00:27:19 +00:00
|
|
|
|
2019-08-23 20:38:02 +00:00
|
|
|
setVolumeRef = c => {
|
|
|
|
this.volume = c;
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
setAudioRef = c => {
|
|
|
|
this.audio = c;
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
if (this.audio) {
|
2023-01-24 15:24:46 +00:00
|
|
|
this.audio.volume = 1;
|
|
|
|
this.audio.muted = false;
|
2019-08-23 20:38:02 +00:00
|
|
|
}
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
setCanvasRef = c => {
|
|
|
|
this.canvas = c;
|
|
|
|
|
2020-07-10 11:57:05 +00:00
|
|
|
this.visualizer.setCanvas(c);
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
componentDidMount () {
|
2020-09-28 11:29:43 +00:00
|
|
|
window.addEventListener('scroll', this.handleScroll);
|
2020-06-24 08:25:32 +00:00
|
|
|
window.addEventListener('resize', this.handleResize, { passive: true });
|
2020-06-21 00:27:19 +00:00
|
|
|
}
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
componentDidUpdate (prevProps, prevState) {
|
2020-08-11 19:01:37 +00:00
|
|
|
if (this.player) {
|
|
|
|
this._setDimensions();
|
|
|
|
}
|
|
|
|
|
2020-07-08 12:54:47 +00:00
|
|
|
if (prevProps.src !== this.props.src || this.state.width !== prevState.width || this.state.height !== prevState.height || prevProps.accentColor !== this.props.accentColor) {
|
2020-07-05 16:28:25 +00:00
|
|
|
this._clear();
|
|
|
|
this._draw();
|
2019-08-23 20:38:02 +00:00
|
|
|
}
|
2020-06-29 11:56:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:39:05 +00:00
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (!is(nextProps.visible, this.props.visible) && nextProps.visible !== undefined) {
|
|
|
|
this.setState({ revealed: nextProps.visible });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:25:32 +00:00
|
|
|
componentWillUnmount () {
|
2020-09-28 11:29:43 +00:00
|
|
|
window.removeEventListener('scroll', this.handleScroll);
|
2020-06-24 08:25:32 +00:00
|
|
|
window.removeEventListener('resize', this.handleResize);
|
2020-09-28 11:29:43 +00:00
|
|
|
|
|
|
|
if (!this.state.paused && this.audio && this.props.deployPictureInPicture) {
|
|
|
|
this.props.deployPictureInPicture('audio', this._pack());
|
|
|
|
}
|
2020-06-24 08:25:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
togglePlay = () => {
|
2020-08-02 16:47:09 +00:00
|
|
|
if (!this.audioContext) {
|
|
|
|
this._initAudioContext();
|
|
|
|
}
|
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
if (this.state.paused) {
|
|
|
|
this.setState({ paused: false }, () => this.audio.play());
|
2019-08-23 20:38:02 +00:00
|
|
|
} else {
|
2020-06-21 00:27:19 +00:00
|
|
|
this.setState({ paused: true }, () => this.audio.pause());
|
2019-08-23 20:38:02 +00:00
|
|
|
}
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-06-21 00:27:19 +00:00
|
|
|
|
2020-06-24 08:25:32 +00:00
|
|
|
handleResize = debounce(() => {
|
|
|
|
if (this.player) {
|
|
|
|
this._setDimensions();
|
|
|
|
}
|
|
|
|
}, 250, {
|
|
|
|
trailing: true,
|
|
|
|
});
|
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
handlePlay = () => {
|
|
|
|
this.setState({ paused: false });
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
if (this.audioContext && this.audioContext.state === 'suspended') {
|
|
|
|
this.audioContext.resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._renderCanvas();
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
handlePause = () => {
|
|
|
|
this.setState({ paused: true });
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
if (this.audioContext) {
|
|
|
|
this.audioContext.suspend();
|
|
|
|
}
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-06-21 00:27:19 +00:00
|
|
|
|
|
|
|
handleProgress = () => {
|
2020-06-24 23:33:01 +00: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-23 20:38:02 +00:00
|
|
|
}
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2019-08-23 20:38:02 +00:00
|
|
|
|
|
|
|
toggleMute = () => {
|
2020-06-21 00:27:19 +00:00
|
|
|
const muted = !this.state.muted;
|
|
|
|
|
|
|
|
this.setState({ muted }, () => {
|
2023-01-24 15:24:46 +00:00
|
|
|
if (this.gainNode) {
|
|
|
|
this.gainNode.gain.value = muted ? 0 : this.state.volume;
|
|
|
|
}
|
2020-06-21 00:27:19 +00:00
|
|
|
});
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2022-08-13 13:39:05 +00:00
|
|
|
toggleReveal = () => {
|
|
|
|
if (this.props.onToggleVisibility) {
|
|
|
|
this.props.onToggleVisibility();
|
|
|
|
} else {
|
|
|
|
this.setState({ revealed: !this.state.revealed });
|
|
|
|
}
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2022-08-13 13:39:05 +00:00
|
|
|
|
2019-08-23 20:38:02 +00: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();
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2019-08-23 20:38:02 +00:00
|
|
|
|
|
|
|
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);
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-06-21 00:27:19 +00: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();
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-06-21 00:27:19 +00:00
|
|
|
|
|
|
|
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();
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-06-21 00:27:19 +00:00
|
|
|
|
|
|
|
handleMouseMove = throttle(e => {
|
|
|
|
const { x } = getPointerPosition(this.seek, e);
|
2020-06-24 23:33:01 +00:00
|
|
|
const currentTime = this.audio.duration * x;
|
2020-06-21 00:27:19 +00:00
|
|
|
|
|
|
|
if (!isNaN(currentTime)) {
|
|
|
|
this.setState({ currentTime }, () => {
|
|
|
|
this.audio.currentTime = currentTime;
|
|
|
|
});
|
|
|
|
}
|
2020-06-24 23:33:01 +00:00
|
|
|
}, 15);
|
2020-06-21 00:27:19 +00:00
|
|
|
|
|
|
|
handleTimeUpdate = () => {
|
|
|
|
this.setState({
|
2020-06-24 23:33:01 +00:00
|
|
|
currentTime: this.audio.currentTime,
|
2020-10-30 12:09:20 +00:00
|
|
|
duration: this.audio.duration,
|
2020-06-21 00:27:19 +00:00
|
|
|
});
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-06-21 00:27:19 +00:00
|
|
|
|
2019-08-23 20:38:02 +00:00
|
|
|
handleMouseVolSlide = throttle(e => {
|
2020-06-23 10:20:14 +00:00
|
|
|
const { x } = getPointerPosition(this.volume, e);
|
2019-08-23 20:38:02 +00:00
|
|
|
|
|
|
|
if(!isNaN(x)) {
|
2020-06-23 10:20:14 +00:00
|
|
|
this.setState({ volume: x }, () => {
|
2023-01-24 15:24:46 +00:00
|
|
|
if (this.gainNode) {
|
|
|
|
this.gainNode.gain.value = this.state.muted ? 0 : x;
|
|
|
|
}
|
2020-06-21 00:27:19 +00:00
|
|
|
});
|
2019-08-23 20:38:02 +00:00
|
|
|
}
|
2020-06-24 23:33:01 +00:00
|
|
|
}, 15);
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-09-28 11:29:43 +00:00
|
|
|
handleScroll = throttle(() => {
|
|
|
|
if (!this.canvas || !this.audio) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { top, height } = this.canvas.getBoundingClientRect();
|
|
|
|
const inView = (top <= (window.innerHeight || document.documentElement.clientHeight)) && (top + height >= 0);
|
|
|
|
|
|
|
|
if (!this.state.paused && !inView) {
|
|
|
|
this.audio.pause();
|
|
|
|
|
|
|
|
if (this.props.deployPictureInPicture) {
|
|
|
|
this.props.deployPictureInPicture('audio', this._pack());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ paused: true });
|
|
|
|
}
|
|
|
|
}, 150, { trailing: true });
|
|
|
|
|
2020-06-23 10:20:14 +00:00
|
|
|
handleMouseEnter = () => {
|
|
|
|
this.setState({ hovered: true });
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-06-23 10:20:14 +00:00
|
|
|
|
|
|
|
handleMouseLeave = () => {
|
|
|
|
this.setState({ hovered: false });
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-06-23 10:20:14 +00:00
|
|
|
|
2020-07-10 20:09:28 +00:00
|
|
|
handleLoadedData = () => {
|
2023-01-24 15:24:46 +00:00
|
|
|
const { autoPlay, currentTime } = this.props;
|
2020-09-28 11:29:43 +00:00
|
|
|
|
|
|
|
if (currentTime) {
|
|
|
|
this.audio.currentTime = currentTime;
|
|
|
|
}
|
|
|
|
|
2020-07-10 20:09:28 +00:00
|
|
|
if (autoPlay) {
|
2020-09-28 11:29:43 +00:00
|
|
|
this.togglePlay();
|
2020-07-10 20:09:28 +00:00
|
|
|
}
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-07-10 20:09:28 +00:00
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
_initAudioContext () {
|
2020-08-02 09:20:17 +00:00
|
|
|
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
|
|
const context = new AudioContext();
|
|
|
|
const source = context.createMediaElementSource(this.audio);
|
2023-01-24 15:24:46 +00:00
|
|
|
const gainNode = context.createGain();
|
|
|
|
|
|
|
|
gainNode.gain.value = this.state.muted ? 0 : this.state.volume;
|
2020-06-21 00:27:19 +00:00
|
|
|
|
2020-07-10 11:57:05 +00:00
|
|
|
this.visualizer.setAudioContext(context, source);
|
2023-01-24 15:24:46 +00:00
|
|
|
source.connect(gainNode);
|
|
|
|
gainNode.connect(context.destination);
|
2020-06-21 00:27:19 +00:00
|
|
|
|
|
|
|
this.audioContext = context;
|
2023-01-24 15:24:46 +00:00
|
|
|
this.gainNode = gainNode;
|
2020-06-21 00:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 10:20:14 +00: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);
|
|
|
|
});
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-06-23 10:20:14 +00:00
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
_renderCanvas () {
|
|
|
|
requestAnimationFrame(() => {
|
2020-07-15 19:07:53 +00:00
|
|
|
if (!this.audio) return;
|
|
|
|
|
2020-06-24 23:33:01 +00:00
|
|
|
this.handleTimeUpdate();
|
2020-06-21 00:27:19 +00:00
|
|
|
this._clear();
|
|
|
|
this._draw();
|
|
|
|
|
|
|
|
if (!this.state.paused) {
|
|
|
|
this._renderCanvas();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-10 11:57:05 +00:00
|
|
|
_clear() {
|
|
|
|
this.visualizer.clear(this.state.width, this.state.height);
|
2020-06-21 00:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 11:57:05 +00:00
|
|
|
_draw() {
|
|
|
|
this.visualizer.draw(this._getCX(), this._getCY(), this._getAccentColor(), this._getRadius(), this._getScaleCoefficient());
|
2020-06-21 00:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_getRadius () {
|
|
|
|
return parseInt(((this.state.height || this.props.height) - (PADDING * this._getScaleCoefficient()) * 2) / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getScaleCoefficient () {
|
|
|
|
return (this.state.height || this.props.height) / 982;
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:25:32 +00:00
|
|
|
_getCX() {
|
|
|
|
return Math.floor(this.state.width / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getCY() {
|
|
|
|
return Math.floor(this._getRadius() + (PADDING * this._getScaleCoefficient()));
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:28:25 +00:00
|
|
|
_getAccentColor () {
|
|
|
|
return this.props.accentColor || '#ffffff';
|
|
|
|
}
|
|
|
|
|
|
|
|
_getBackgroundColor () {
|
|
|
|
return this.props.backgroundColor || '#000000';
|
|
|
|
}
|
|
|
|
|
|
|
|
_getForegroundColor () {
|
|
|
|
return this.props.foregroundColor || '#ffffff';
|
2020-06-21 00:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 13:24:54 +00:00
|
|
|
seekBy (time) {
|
|
|
|
const currentTime = this.audio.currentTime + time;
|
|
|
|
|
|
|
|
if (!isNaN(currentTime)) {
|
|
|
|
this.setState({ currentTime }, () => {
|
|
|
|
this.audio.currentTime = currentTime;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleAudioKeyDown = e => {
|
|
|
|
// On the audio element or the seek bar, we can safely use the space bar
|
|
|
|
// for playback control because there are no buttons to press
|
|
|
|
|
|
|
|
if (e.key === ' ') {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.togglePlay();
|
|
|
|
}
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-11-15 13:24:54 +00:00
|
|
|
|
|
|
|
handleKeyDown = e => {
|
|
|
|
switch(e.key) {
|
|
|
|
case 'k':
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.togglePlay();
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.toggleMute();
|
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.seekBy(-10);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.seekBy(10);
|
|
|
|
break;
|
|
|
|
}
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2020-11-15 13:24:54 +00:00
|
|
|
|
2019-08-23 20:38:02 +00:00
|
|
|
render () {
|
2023-02-26 19:13:27 +00:00
|
|
|
const { src, intl, alt, lang, editable, autoPlay, sensitive, blurhash } = this.props;
|
2022-08-13 13:39:05 +00:00
|
|
|
const { paused, muted, volume, currentTime, duration, buffer, dragging, revealed } = this.state;
|
2020-09-28 11:29:43 +00:00
|
|
|
const progress = Math.min((currentTime / duration) * 100, 100);
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2022-08-13 13:39:05 +00:00
|
|
|
let warning;
|
|
|
|
if (sensitive) {
|
|
|
|
warning = <FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' />;
|
|
|
|
} else {
|
|
|
|
warning = <FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' />;
|
|
|
|
}
|
|
|
|
|
2019-08-23 20:38:02 +00:00
|
|
|
return (
|
2022-08-13 13:39:05 +00:00
|
|
|
<div className={classNames('audio-player', { editable, inactive: !revealed })} 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} tabIndex='0' onKeyDown={this.handleKeyDown}>
|
|
|
|
|
|
|
|
<Blurhash
|
|
|
|
hash={blurhash}
|
|
|
|
className={classNames('media-gallery__preview', {
|
|
|
|
'media-gallery__preview--hidden': revealed,
|
|
|
|
})}
|
|
|
|
dummy={!useBlurhash}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{(revealed || editable) && <audio
|
2020-06-21 00:27:19 +00:00
|
|
|
src={src}
|
|
|
|
ref={this.setAudioRef}
|
2020-07-10 20:09:28 +00:00
|
|
|
preload={autoPlay ? 'auto' : 'none'}
|
2020-06-21 00:27:19 +00:00
|
|
|
onPlay={this.handlePlay}
|
|
|
|
onPause={this.handlePause}
|
|
|
|
onProgress={this.handleProgress}
|
2020-07-10 20:09:28 +00:00
|
|
|
onLoadedData={this.handleLoadedData}
|
2020-06-22 17:24:16 +00:00
|
|
|
crossOrigin='anonymous'
|
2022-08-13 13:39:05 +00:00
|
|
|
/>}
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
<canvas
|
|
|
|
role='button'
|
2020-11-15 13:24:54 +00:00
|
|
|
tabIndex='0'
|
2020-06-21 00:27:19 +00:00
|
|
|
className='audio-player__canvas'
|
|
|
|
width={this.state.width}
|
|
|
|
height={this.state.height}
|
2020-07-05 16:28:25 +00:00
|
|
|
style={{ width: '100%', position: 'absolute', top: 0, left: 0 }}
|
2020-06-21 00:27:19 +00:00
|
|
|
ref={this.setCanvasRef}
|
2020-07-05 16:28:25 +00:00
|
|
|
onClick={this.togglePlay}
|
2020-11-15 13:24:54 +00:00
|
|
|
onKeyDown={this.handleAudioKeyDown}
|
2020-07-05 16:28:25 +00:00
|
|
|
title={alt}
|
|
|
|
aria-label={alt}
|
2023-02-26 19:13:27 +00:00
|
|
|
lang={lang}
|
2020-06-21 00:27:19 +00:00
|
|
|
/>
|
|
|
|
|
2022-08-13 13:39:05 +00:00
|
|
|
<div className={classNames('spoiler-button', { 'spoiler-button--hidden': revealed || editable })}>
|
|
|
|
<button type='button' className='spoiler-button__overlay' onClick={this.toggleReveal}>
|
|
|
|
<span className='spoiler-button__overlay__label'>{warning}</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{(revealed || editable) && <img
|
2020-06-21 00:27:19 +00:00
|
|
|
src={this.props.poster}
|
|
|
|
alt=''
|
|
|
|
width={(this._getRadius() - TICK_SIZE) * 2}
|
|
|
|
height={(this._getRadius() - TICK_SIZE) * 2}
|
2020-06-24 08:25:32 +00:00
|
|
|
style={{ position: 'absolute', left: this._getCX(), top: this._getCY(), transform: 'translate(-50%, -50%)', borderRadius: '50%', pointerEvents: 'none' }}
|
2022-08-13 13:39:05 +00:00
|
|
|
/>}
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-06-21 00:27:19 +00:00
|
|
|
<div className='video-player__seek' onMouseDown={this.handleMouseDown} ref={this.setSeekRef}>
|
|
|
|
<div className='video-player__seek__buffer' style={{ width: `${buffer}%` }} />
|
2020-07-05 16:28:25 +00:00
|
|
|
<div className='video-player__seek__progress' style={{ width: `${progress}%`, backgroundColor: this._getAccentColor() }} />
|
2020-06-21 00:27:19 +00:00
|
|
|
|
|
|
|
<span
|
|
|
|
className={classNames('video-player__seek__handle', { active: dragging })}
|
|
|
|
tabIndex='0'
|
2020-07-05 16:28:25 +00:00
|
|
|
style={{ left: `${progress}%`, backgroundColor: this._getAccentColor() }}
|
2020-11-15 13:24:54 +00:00
|
|
|
onKeyDown={this.handleAudioKeyDown}
|
2020-06-21 00:27:19 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2019-08-23 20:38:02 +00:00
|
|
|
<div className='video-player__controls active'>
|
|
|
|
<div className='video-player__buttons-bar'>
|
|
|
|
<div className='video-player__buttons left'>
|
2020-10-27 01:58:47 +00:00
|
|
|
<button type='button' title={intl.formatMessage(paused ? messages.play : messages.pause)} aria-label={intl.formatMessage(paused ? messages.play : messages.pause)} className='player-button' 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)} className='player-button' onClick={this.toggleMute}><Icon id={muted ? 'volume-off' : 'volume-up'} fixedWidth /></button>
|
2019-08-23 20:38:02 +00:00
|
|
|
|
2020-06-23 10:20:14 +00:00
|
|
|
<div className={classNames('video-player__volume', { active: this.state.hovered })} ref={this.setVolumeRef} onMouseDown={this.handleVolumeMouseDown}>
|
2020-07-05 16:28:25 +00:00
|
|
|
<div className='video-player__volume__current' style={{ width: `${volume * 100}%`, backgroundColor: this._getAccentColor() }} />
|
2019-08-23 20:38:02 +00:00
|
|
|
|
|
|
|
<span
|
2020-10-27 01:58:47 +00:00
|
|
|
className='video-player__volume__handle'
|
2019-08-23 20:38:02 +00:00
|
|
|
tabIndex='0'
|
2020-07-05 16:28:25 +00:00
|
|
|
style={{ left: `${volume * 100}%`, backgroundColor: this._getAccentColor() }}
|
2019-08-23 20:38:02 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2020-06-23 10:20:14 +00:00
|
|
|
<span className='video-player__time'>
|
2020-06-24 23:33:01 +00:00
|
|
|
<span className='video-player__time-current'>{formatTime(Math.floor(currentTime))}</span>
|
2019-08-23 20:38:02 +00:00
|
|
|
<span className='video-player__time-sep'>/</span>
|
2020-10-30 12:09:20 +00:00
|
|
|
<span className='video-player__time-total'>{formatTime(Math.floor(this.state.duration || this.props.duration))}</span>
|
2019-08-23 20:38:02 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
2019-10-25 09:48:20 +00:00
|
|
|
|
|
|
|
<div className='video-player__buttons right'>
|
2022-08-13 13:39:05 +00:00
|
|
|
{!editable && <button type='button' title={intl.formatMessage(messages.hide)} aria-label={intl.formatMessage(messages.hide)} className='player-button' onClick={this.toggleReveal}><Icon id='eye-slash' fixedWidth /></button>}
|
2020-10-27 01:58:47 +00:00
|
|
|
<a title={intl.formatMessage(messages.download)} aria-label={intl.formatMessage(messages.download)} className='video-player__download__icon player-button' href={this.props.src} download>
|
|
|
|
<Icon id={'download'} fixedWidth />
|
|
|
|
</a>
|
2019-10-25 09:48:20 +00:00
|
|
|
</div>
|
2019-08-23 20:38:02 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-03-24 22:15:25 +00:00
|
|
|
|
|
|
|
export default injectIntl(Audio);
|