61 lines
1.7 KiB
TypeScript
Raw Normal View History

2018-03-11 02:00:06 +00:00
import { AnimeNotifier } from "../AnimeNotifier"
2018-03-11 03:45:01 +00:00
var audioContext: AudioContext
var audioNode: AudioBufferSourceNode
2018-03-11 02:00:06 +00:00
// Play audio file
export function playAudio(arn: AnimeNotifier, button: HTMLButtonElement) {
2018-03-11 03:45:01 +00:00
if(!audioContext) {
audioContext = new AudioContext()
}
if(audioNode) {
audioNode.stop()
2018-03-11 02:00:06 +00:00
}
2018-03-11 03:45:01 +00:00
let request = new XMLHttpRequest()
request.open("GET", button.dataset.src, true)
request.responseType = "arraybuffer"
request.onload = () => {
audioContext.decodeAudioData(request.response, buffer => {
console.log("play")
audioNode = audioContext.createBufferSource()
audioNode.buffer = buffer
audioNode.connect(audioContext.destination)
audioNode.start(0)
2018-03-11 03:51:10 +00:00
audioNode.onended = () => {
document.getElementById("audio-player").classList.add("fade-out")
}
2018-03-11 03:45:01 +00:00
}, console.error)
}
request.send()
// Show audio player
document.getElementById("audio-player").classList.remove("fade-out")
document.getElementById("audio-player-play").classList.add("fade-out")
document.getElementById("audio-player-pause").classList.remove("fade-out")
}
// Pause audio
export function pauseAudio(arn: AnimeNotifier, button: HTMLButtonElement) {
if(!audioNode) {
return
}
audioNode.playbackRate.setValueAtTime(0.0, 0)
document.getElementById("audio-player-play").classList.remove("fade-out")
document.getElementById("audio-player-pause").classList.add("fade-out")
}
// Resume audio
export function resumeAudio(arn: AnimeNotifier, button: HTMLButtonElement) {
if(!audioNode) {
return
}
audioNode.playbackRate.setValueAtTime(1.0, 0)
document.getElementById("audio-player-play").classList.add("fade-out")
document.getElementById("audio-player-pause").classList.remove("fade-out")
2018-03-11 02:00:06 +00:00
}