From eecb14ecba5b99893dbbf3329dc772dbe06f8b1b Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Thu, 27 Jan 2022 14:41:26 -0500 Subject: [PATCH] Fix seekable songs --- assets/js/app.js | 46 +++++++------------ .../controllers/file_controller.ex | 10 ++-- 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index d908069..f2bfd1d 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -97,13 +97,8 @@ Hooks.AudioPlayer = { mounted(){ this.playbackBeganAt = null this.player = this.el.querySelector("audio") - this.player.addEventListener("ended", () => console.log("player: ended")) - this.player.addEventListener("stalled", () => console.log("player: stalled")) - this.player.addEventListener("suspend", () => console.log("player: suspend")) - this.player.addEventListener("waiting", () => console.log("player: waiting")) - this.playerDuration = 0 this.currentTime = this.el.querySelector("#player-time") - this.durationText = this.el.querySelector("#player-duration") + this.duration = this.el.querySelector("#player-duration") this.progress = this.el.querySelector("#player-progress") let enableAudio = () => { if(this.player.src){ @@ -121,14 +116,13 @@ Hooks.AudioPlayer = { this.play() } }) - this.handleEvent("play", ({url, token, duration, elapsed}) => { + this.handleEvent("play", ({url, token, elapsed}) => { this.playbackBeganAt = nowSeconds() - elapsed let currentSrc = this.player.src.split("?")[0] - this.playerDuration = duration if(currentSrc === url && this.player.paused){ this.play({sync: true}) } else if(currentSrc !== url) { - this.player.src = `${url}?token=${token}&proxy` + this.player.src = `${url}?token=${token}` this.play({sync: true}) } }) @@ -136,16 +130,16 @@ Hooks.AudioPlayer = { this.handleEvent("stop", () => this.stop()) }, - play(opts = {}){ - console.log("play") - let {sync} = opts - clearInterval(this.progressTimer) + clearNextTimer(){ clearTimeout(this.nextTimer) + this.nextTimer = null + }, + + play(opts = {}){ + let {sync} = opts + this.clearNextTimer() this.player.play().then(() => { - if(sync){ - console.log("sync", nowSeconds() - this.playbackBeganAt) - this.player.currentTime = nowSeconds() - this.playbackBeganAt - } + if(sync){ this.player.currentTime = nowSeconds() - this.playbackBeganAt } this.progressTimer = setInterval(() => this.updateProgress(), 100) }, error => { if(error.name === "NotAllowedError"){ @@ -156,32 +150,27 @@ Hooks.AudioPlayer = { pause(){ clearInterval(this.progressTimer) - clearTimeout(this.nextTimer) this.player.pause() }, stop(){ clearInterval(this.progressTimer) - clearTimeout(this.nextTimer) this.player.pause() this.player.currentTime = 0 this.updateProgress() - this.durationText.innerText = "" + this.duration.innerText = "" this.currentTime.innerText = "" }, updateProgress(){ - if(this.playerDuration === 0){ return false } - if(Math.ceil(this.player.currentTime) >= Math.floor(this.playerDuration)){ + if(isNaN(this.player.duration)){ return false } + if(!this.nextTimer && this.player.currentTime >= this.player.duration){ clearInterval(this.progressTimer) - this.player.pause() - this.playerDuration = 0 - console.log("next_song_auto") - this.nextTimer = setTimeout(() => this.pushEvent("next_song_auto"), rand(1000, 3000)) + this.nextTimer = setTimeout(() => this.pushEvent("next_song_auto"), rand(0, 1500)) return } - this.progress.style.width = `${(this.player.currentTime / (this.playerDuration) * 100)}%` - this.durationText.innerText = this.formatTime(this.playerDuration) + this.progress.style.width = `${(this.player.currentTime / (this.player.duration) * 100)}%` + this.duration.innerText = this.formatTime(this.player.duration) this.currentTime.innerText = this.formatTime(this.player.currentTime) }, @@ -226,4 +215,3 @@ liveSocket.connect() // >> liveSocket.disableLatencySim() window.liveSocket = liveSocket - diff --git a/lib/live_beats_web/controllers/file_controller.ex b/lib/live_beats_web/controllers/file_controller.ex index 01e593a..22812a1 100644 --- a/lib/live_beats_web/controllers/file_controller.ex +++ b/lib/live_beats_web/controllers/file_controller.ex @@ -8,14 +8,13 @@ defmodule LiveBeatsWeb.FileController do require Logger - def show(conn, %{"id" => filename_uuid, "token" => token} = params) do + def show(conn, %{"id" => filename_uuid, "token" => token}) do path = MediaLibrary.local_filepath(filename_uuid) mime_type = MIME.from_path(path) case Phoenix.Token.decrypt(conn, "file", token, max_age: :timer.minutes(1)) do {:ok, %{vsn: 1, uuid: ^filename_uuid, ip: ip, size: size}} -> - # if local_file?(filename_uuid, ip) do - if !params["proxy"] do + if local_file?(filename_uuid, ip) do Logger.info("serving file from #{server_ip()}") do_send_file(conn, path) else @@ -42,8 +41,7 @@ defmodule LiveBeatsWeb.FileController do defp proxy_file(conn, ip, mime_type, content_length) do uri = conn |> request_url() |> URI.parse() port = LiveBeatsWeb.Endpoint.config(:http)[:port] - # path = uri.path <> "?" <> uri.query <> "&from=#{server_ip()}" - path = uri.path <> "?" <> String.replace(uri.query, "&proxy", "") <> "&from=#{server_ip()}" + path = uri.path <> "?" <> uri.query <> "&from=#{server_ip()}" {:ok, ipv6} = :inet.parse_address(String.to_charlist(ip)) {:ok, req} = Mint.HTTP.connect(:http, ipv6, port, file_server_opts()) {:ok, req, request_ref} = Mint.HTTP.request(req, "GET", path, [], "") @@ -51,7 +49,7 @@ defmodule LiveBeatsWeb.FileController do conn |> put_resp_header("content-type", mime_type) |> put_resp_header("accept-ranges", "bytes") - |> put_resp_header("content-length", IO.inspect(to_string(content_length))) + |> put_resp_header("content-length", to_string(content_length)) |> send_chunked(200) |> stream(req, request_ref) end