2019-11-11 17:39:56 +01:00
|
|
|
import React, { Component } from "react";
|
2019-11-11 19:06:35 +01:00
|
|
|
import {
|
|
|
|
Toolbar,
|
|
|
|
IconButton,
|
|
|
|
withStyles,
|
|
|
|
LinearProgress,
|
|
|
|
Tooltip
|
|
|
|
} from "@material-ui/core";
|
|
|
|
import { LinkableIconButton } from "../../interfaces/overrides";
|
|
|
|
|
|
|
|
import FastRewindIcon from "@material-ui/icons/FastRewind";
|
|
|
|
import FastForwardIcon from "@material-ui/icons/FastForward";
|
|
|
|
import PlayArrowIcon from "@material-ui/icons/PlayArrow";
|
|
|
|
import PauseIcon from "@material-ui/icons/Pause";
|
|
|
|
import CloudDownloadIcon from "@material-ui/icons/CloudDownload";
|
2019-11-11 17:39:56 +01:00
|
|
|
|
|
|
|
import { styles } from "./AudioPlayer.styles";
|
|
|
|
|
2019-11-11 19:06:35 +01:00
|
|
|
interface IAudioPlayerProps {
|
|
|
|
src: string;
|
|
|
|
id: string;
|
|
|
|
classes: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IAudioPlayerState {
|
|
|
|
src: string;
|
|
|
|
elementId: string;
|
|
|
|
playing: boolean;
|
|
|
|
progress: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AudioPlayer extends Component<IAudioPlayerProps, IAudioPlayerState> {
|
|
|
|
constructor(props: any) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
src: this.props.src,
|
|
|
|
elementId: "audioplayer-" + this.props.id,
|
|
|
|
playing: false,
|
|
|
|
progress: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
let audioPlayerElement = this.getAudioPlayer();
|
|
|
|
|
|
|
|
if (audioPlayerElement) {
|
|
|
|
audioPlayerElement.ontimeupdate = () => {
|
|
|
|
let music = audioPlayerElement as HTMLAudioElement;
|
|
|
|
let progress = 100 * (music.currentTime / music.duration);
|
|
|
|
this.setState({ progress });
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getAudioPlayer(): HTMLAudioElement | null {
|
|
|
|
return document.getElementById(
|
|
|
|
this.state.elementId
|
|
|
|
) as HTMLAudioElement | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleAudio() {
|
|
|
|
let audioPlayerElement = this.getAudioPlayer();
|
|
|
|
|
|
|
|
if (audioPlayerElement && this.state.playing) {
|
|
|
|
audioPlayerElement.pause();
|
|
|
|
this.setState({ playing: false });
|
|
|
|
} else if (audioPlayerElement) {
|
|
|
|
audioPlayerElement.play();
|
|
|
|
this.setState({ playing: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fastForward() {
|
|
|
|
let audioPlayerElement = this.getAudioPlayer();
|
|
|
|
|
|
|
|
if (audioPlayerElement) {
|
|
|
|
audioPlayerElement.currentTime += 15.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rewind() {
|
|
|
|
let audioPlayerElement = this.getAudioPlayer();
|
|
|
|
|
|
|
|
if (audioPlayerElement) {
|
|
|
|
audioPlayerElement.currentTime -= 15.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 17:39:56 +01:00
|
|
|
render() {
|
2019-11-11 19:06:35 +01:00
|
|
|
const { classes } = this.props;
|
|
|
|
return (
|
|
|
|
<div className={classes.root}>
|
|
|
|
<audio
|
|
|
|
id={this.state.elementId}
|
|
|
|
src={this.state.src}
|
|
|
|
autoPlay={false}
|
|
|
|
/>
|
|
|
|
<Toolbar>
|
|
|
|
<Tooltip title="Rewind by 15s">
|
|
|
|
<IconButton onClick={() => this.rewind()}>
|
|
|
|
<FastRewindIcon />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
<Tooltip title={this.state.playing ? "Pause" : "Play"}>
|
|
|
|
<IconButton onClick={() => this.toggleAudio()}>
|
|
|
|
{this.state.playing ? (
|
|
|
|
<PauseIcon />
|
|
|
|
) : (
|
|
|
|
<PlayArrowIcon />
|
|
|
|
)}
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
<Tooltip title="Fast-forward by 15s">
|
|
|
|
<IconButton onClick={() => this.fastForward()}>
|
|
|
|
<FastForwardIcon />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
<LinearProgress
|
|
|
|
className={classes.progressBar}
|
|
|
|
variant="determinate"
|
|
|
|
color={"secondary"}
|
|
|
|
value={this.state.progress}
|
|
|
|
/>
|
|
|
|
<Tooltip title="Download">
|
|
|
|
<IconButton
|
|
|
|
href={this.state.src}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer nofollower"
|
|
|
|
className={classes.download}
|
|
|
|
>
|
|
|
|
<CloudDownloadIcon />
|
|
|
|
</IconButton>
|
|
|
|
</Tooltip>
|
|
|
|
</Toolbar>
|
|
|
|
</div>
|
|
|
|
);
|
2019-11-11 17:39:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withStyles(styles)(AudioPlayer);
|