2023-08-09 16:04:12 +00:00
|
|
|
class AudioContexthehe {
|
2024-02-12 15:28:54 +00:00
|
|
|
state = "suspended"
|
|
|
|
constructor() {}
|
|
|
|
resume() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.state = "running"
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
}
|
2023-08-09 16:04:12 +00:00
|
|
|
}
|
|
|
|
class AudioElementHehe {
|
2024-02-12 15:28:54 +00:00
|
|
|
constructor() {}
|
|
|
|
play() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
console.log("playing!")
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pause() {
|
|
|
|
console.log("Pausing!")
|
|
|
|
}
|
2023-08-09 16:04:12 +00:00
|
|
|
}
|
2024-02-12 15:28:54 +00:00
|
|
|
const audio_context = new AudioContexthehe()
|
|
|
|
const audio_element = new AudioElementHehe()
|
2023-08-09 16:04:12 +00:00
|
|
|
let is_playing = false
|
|
|
|
try_play_toggle_async()
|
|
|
|
|
|
|
|
function try_play_toggle_async() {
|
2024-02-12 15:28:54 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (audio_context.state !== "running") {
|
|
|
|
audio_context.resume().catch((e) => reject(e))
|
|
|
|
}
|
|
|
|
if (audio_element.paused) {
|
|
|
|
audio_element.play().then(
|
|
|
|
(s) => {
|
|
|
|
is_playing = true
|
|
|
|
resolve(s)
|
|
|
|
},
|
|
|
|
(r) => {
|
|
|
|
is_playing = false
|
|
|
|
reject(r)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
audio_element.pause()
|
|
|
|
is_playing = false
|
|
|
|
resolve(null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|