Improved audio player

This commit is contained in:
Eduard Urbach 2018-03-11 17:20:52 +01:00
parent 99a0e9217a
commit 5e8727da1d
2 changed files with 34 additions and 16 deletions

View File

@ -22,7 +22,7 @@
width 100% width 100%
height 200px height 200px
filter brightness(100%) filter brightness(100%)
transition filter 250ms ease transition filter transition-speed ease
.soundtrack-play-button .soundtrack-play-button
position absolute position absolute
@ -36,6 +36,7 @@
color rgba(0, 0, 0, 0.15) color rgba(0, 0, 0, 0.15)
background rgba(255, 255, 255, 0.9) background rgba(255, 255, 255, 0.9)
pointer-events none pointer-events none
box-shadow outline-shadow-medium
.icon-play .icon-play
transform translateX(27%) transform translateX(27%)
@ -102,13 +103,16 @@ animation change-color
opacity 1 opacity 1
.soundtrack-image .soundtrack-image
filter brightness(0) filter brightness(0) !important
:hover :hover
.soundtrack-play-button .soundtrack-play-button
color button-hover-color color button-hover-color
background button-hover-background background button-hover-background
.soundtrack-image
filter brightness(50%)
.soundtrack-footer .soundtrack-footer
text-align center text-align center
margin-bottom 1rem margin-bottom 1rem

View File

@ -2,7 +2,7 @@ import { AnimeNotifier } from "../AnimeNotifier"
var audioContext: AudioContext var audioContext: AudioContext
var audioNode: AudioBufferSourceNode var audioNode: AudioBufferSourceNode
var playID = 0 var playId = 0
var audioPlayer = document.getElementById("audio-player") var audioPlayer = document.getElementById("audio-player")
var audioPlayerPlay = document.getElementById("audio-player-play") var audioPlayerPlay = document.getElementById("audio-player-play")
var audioPlayerPause = document.getElementById("audio-player-pause") var audioPlayerPause = document.getElementById("audio-player-pause")
@ -13,7 +13,8 @@ export function playAudio(arn: AnimeNotifier, element: HTMLElement) {
audioContext = new AudioContext() audioContext = new AudioContext()
} }
playID++ playId++
let currentPlayId = playId
// Stop current track // Stop current track
stopAudio(arn) stopAudio(arn)
@ -27,18 +28,26 @@ export function playAudio(arn: AnimeNotifier, element: HTMLElement) {
request.responseType = "arraybuffer" request.responseType = "arraybuffer"
request.onload = () => { request.onload = () => {
if(currentPlayId !== playId) {
return
}
audioContext.decodeAudioData(request.response, buffer => { audioContext.decodeAudioData(request.response, buffer => {
if(currentPlayId !== playId) {
return
}
audioNode = audioContext.createBufferSource() audioNode = audioContext.createBufferSource()
audioNode.buffer = buffer audioNode.buffer = buffer
audioNode.connect(audioContext.destination) audioNode.connect(audioContext.destination)
audioNode.start(0) audioNode.start(0)
let currentPlayCount = playID
audioNode.onended = (event: MediaStreamErrorEvent) => { audioNode.onended = (event: MediaStreamErrorEvent) => {
if(currentPlayCount === playID) { if(currentPlayId !== playId) {
stopAudio(arn) return
} }
stopAudio(arn)
} }
}, console.error) }, console.error)
} }
@ -53,14 +62,6 @@ export function playAudio(arn: AnimeNotifier, element: HTMLElement) {
// Stop audio // Stop audio
export function stopAudio(arn: AnimeNotifier) { export function stopAudio(arn: AnimeNotifier) {
if(!audioNode) {
return
}
audioNode.stop()
audioNode.disconnect()
audioNode = null
arn.currentSoundTrackId = undefined arn.currentSoundTrackId = undefined
// Remove CSS class "playing" // Remove CSS class "playing"
@ -72,6 +73,14 @@ export function stopAudio(arn: AnimeNotifier) {
// Fade out sidebar player // Fade out sidebar player
audioPlayer.classList.add("fade-out") audioPlayer.classList.add("fade-out")
if(!audioNode) {
return
}
audioNode.stop()
audioNode.disconnect()
audioNode = null
} }
// Toggle audio // Toggle audio
@ -85,6 +94,11 @@ export function toggleAudio(arn: AnimeNotifier, element: HTMLElement) {
} }
} }
// Set volume
export function setVolume(arn: AnimeNotifier, element: HTMLElement) {
}
// Pause audio // Pause audio
export function pauseAudio(arn: AnimeNotifier, button: HTMLButtonElement) { export function pauseAudio(arn: AnimeNotifier, button: HTMLButtonElement) {
if(!audioNode) { if(!audioNode) {