Skip to content

Instantly share code, notes, and snippets.

@joake
Created October 9, 2019 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joake/56cd4254d2c9d10ab19cf387a8e918cd to your computer and use it in GitHub Desktop.
Save joake/56cd4254d2c9d10ab19cf387a8e918cd to your computer and use it in GitHub Desktop.
import { Fragment, useEffect, useRef } from 'react';
import { useEditorState, Icon } from 'vev';
export default function({ text }: Props) {
const audioRef = useRef<HTMLAudioElement>(null);
const handlePlay = () => {
if(audioRef.current) {
audioRef.current.play(); // plays audio
}
}
const handlePause = () => {
if(audioRef.current) {
audioRef.current.pause(); // pause audio
}
}
const handleTimeUpdate = (updateEvent:Event) => {
console.log("current time", audioRef.current.currentTime); // use this to create a scrubber / timeline
}
useEffect(() => {
console.log("got audio");
audioRef.current.addEventListener('timeupdate', handleTimeUpdate);
}, [audioRef.current])
return (
<Fragment>
<div className="controls fill">
<Icon d="play" onClick={handlePlay} />
<Icon d="pause" onClick={handlePause} />
</div>
<audio ref={audioRef}>
<source src="https://www.w3schools.com/html/horse.ogg" type="audio/ogg" />
<source src="https://www.w3schools.com/html/horse.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>
</Fragment>
);
}
:host{
}
.controls {
display: vev(flex);
flex-direction: vev(row);
align-items: vev(center);
background: vev();
font-size: vev(24px);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment