Improved soundtrack components

This commit is contained in:
Eduard Urbach 2018-03-11 20:22:33 +01:00
parent ee071c3fe8
commit 90be415d9a
5 changed files with 75 additions and 25 deletions

View File

@ -1,5 +1,5 @@
component AudioPlayer component AudioPlayer
#audio-player.fade-out #audio-player
.audio-player-play-pause-container .audio-player-play-pause-container
button#audio-player-play.action(data-action="resumeAudio", data-trigger="click") button#audio-player-play.action(data-action="resumeAudio", data-trigger="click")
RawIcon("play") RawIcon("play")

View File

@ -1,21 +1,21 @@
component SoundTrack(track *arn.SoundTrack) component SoundTrack(track *arn.SoundTrack)
SoundTrackMedia(track, track.Media[0])
component SoundTrackMedia(track *arn.SoundTrack, media *arn.ExternalMedia)
.soundtrack.mountable(id=track.ID) .soundtrack.mountable(id=track.ID)
SoundTrackContent(track, media) SoundTrackContent(track)
SoundTrackFooter(track) SoundTrackFooter(track)
component SoundTrackContent(track *arn.SoundTrack, media *arn.ExternalMedia) component SoundTrackContent(track *arn.SoundTrack)
.soundtrack-content .soundtrack-content
if track.MainAnime() != nil if track.MainAnime() != nil
a.soundtrack-anime-link.ajax(href="/anime/" + track.MainAnime().ID) a.soundtrack-anime-link.ajax(href="/anime/" + track.MainAnime().ID)
img.soundtrack-anime-image.lazy(data-src=track.MainAnime().Image("medium"), data-webp="true", alt=track.MainAnime().Title.Canonical, title=track.MainAnime().Title.Canonical) img.soundtrack-anime-image.lazy(data-src=track.MainAnime().Image("medium"), data-webp="true", alt=track.MainAnime().Title.Canonical, title=track.MainAnime().Title.Canonical)
if track.File != "" && media.Service == "Youtube" SoundTrackMedia(track)
component SoundTrackMedia(track *arn.SoundTrack)
if track.File != ""
.soundtrack-media .soundtrack-media
.soundtrack-play-area.action(data-action="toggleAudio", data-trigger="click", data-audio-src="https://notify.moe/audio/" + track.File, data-soundtrack-id=track.ID) .soundtrack-play-area.action(data-action="toggleAudio", data-trigger="click", data-audio-src="https://notify.moe/audio/" + track.File, data-soundtrack-id=track.ID)
img.soundtrack-image.lazy(data-src="https://img.youtube.com/vi/" + media.ServiceID + "/maxresdefault.jpg", alt=track.Title) img.soundtrack-image.lazy(data-src="https://img.youtube.com/vi/" + track.MediaByService("Youtube")[0].ServiceID + "/maxresdefault.jpg", alt=track.Title)
button.soundtrack-play-button button.soundtrack-play-button
RawIcon("play") RawIcon("play")
@ -24,7 +24,7 @@ component SoundTrackContent(track *arn.SoundTrack, media *arn.ExternalMedia)
.visualizer-box.visualizer-box-2 .visualizer-box.visualizer-box-2
.visualizer-box.visualizer-box-3 .visualizer-box.visualizer-box-3
else else
ExternalMedia(media) ExternalMedia(track.Media[0])
component SoundTrackFooter(track *arn.SoundTrack) component SoundTrackFooter(track *arn.SoundTrack)
.soundtrack-footer .soundtrack-footer

View File

@ -246,6 +246,8 @@ func Configure(app *aero.Application) {
app.Get("/api/test/notification", notifications.Test) app.Get("/api/test/notification", notifications.Test)
app.Get("/api/count/notifications/unseen", notifications.CountUnseen) app.Get("/api/count/notifications/unseen", notifications.CountUnseen)
app.Get("/api/mark/notifications/seen", notifications.MarkNotificationsAsSeen) app.Get("/api/mark/notifications/seen", notifications.MarkNotificationsAsSeen)
app.Get("/api/random/soundtrack", soundtrack.Random)
app.Get("/api/next/soundtrack", soundtrack.Next)
// Legal stuff // Legal stuff
l.Page("/terms", terms.Get) l.Page("/terms", terms.Get)

View File

@ -0,0 +1,29 @@
package soundtrack
import (
"math/rand"
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
)
// Random returns a random soundtrack.
func Random(ctx *aero.Context) string {
tracks := arn.AllSoundTracks()
index := rand.Intn(len(tracks))
track := tracks[index]
return ctx.JSON(track)
}
// Next returns the next soundtrack for the audio player.
func Next(ctx *aero.Context) string {
tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
return track.File != ""
})
index := rand.Intn(len(tracks))
track := tracks[index]
return ctx.JSON(track)
}

View File

@ -9,8 +9,13 @@ 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")
// Play audio file // Play audio
export function playAudio(arn: AnimeNotifier, element: HTMLElement) { export function playAudio(arn: AnimeNotifier, element: HTMLElement) {
playAudioFile(arn, element.dataset.soundtrackId, element.dataset.audioSrc)
}
// Play audio file
function playAudioFile(arn: AnimeNotifier, trackId: string, trackUrl: string) {
if(!audioContext) { if(!audioContext) {
audioContext = new AudioContext() audioContext = new AudioContext()
gainNode = audioContext.createGain() gainNode = audioContext.createGain()
@ -23,12 +28,12 @@ export function playAudio(arn: AnimeNotifier, element: HTMLElement) {
// Stop current track // Stop current track
stopAudio(arn) stopAudio(arn)
arn.currentSoundTrackId = element.dataset.soundtrackId arn.currentSoundTrackId = trackId
element.classList.add("playing") arn.markPlayingSoundTrack()
// Request // Request
let request = new XMLHttpRequest() let request = new XMLHttpRequest()
request.open("GET", element.dataset.audioSrc, true) request.open("GET", trackUrl, true)
request.responseType = "arraybuffer" request.responseType = "arraybuffer"
request.onload = () => { request.onload = () => {
@ -52,7 +57,8 @@ export function playAudio(arn: AnimeNotifier, element: HTMLElement) {
return return
} }
stopAudio(arn) playNextTrack(arn)
// stopAudio(arn)
} }
}, console.error) }, console.error)
} }
@ -101,6 +107,17 @@ export function toggleAudio(arn: AnimeNotifier, element: HTMLElement) {
} }
} }
// Play next track
export async function playNextTrack(arn: AnimeNotifier) {
// Get random track
let response = await fetch("/api/next/soundtrack")
let track = await response.json()
playAudioFile(arn, track.id, "https://notify.moe/audio/" + track.file)
return track
}
// Set volume // Set volume
export function setVolume(arn: AnimeNotifier, element: HTMLInputElement) { export function setVolume(arn: AnimeNotifier, element: HTMLInputElement) {
volume = parseFloat(element.value) / 100.0 volume = parseFloat(element.value) / 100.0
@ -123,8 +140,10 @@ export function pauseAudio(arn: AnimeNotifier, button: HTMLButtonElement) {
} }
// Resume audio // Resume audio
export function resumeAudio(arn: AnimeNotifier, button: HTMLButtonElement) { export async function resumeAudio(arn: AnimeNotifier, button: HTMLButtonElement) {
if(!audioNode) { if(!audioNode) {
let track = await playNextTrack(arn)
arn.statusMessage.showInfo("Now playing: " + track.title)
return return
} }