Fix seekable songs

This commit is contained in:
Chris McCord 2022-01-27 14:41:26 -05:00
parent 214ec50f0e
commit eecb14ecba
2 changed files with 21 additions and 35 deletions

View file

@ -97,13 +97,8 @@ Hooks.AudioPlayer = {
mounted(){ mounted(){
this.playbackBeganAt = null this.playbackBeganAt = null
this.player = this.el.querySelector("audio") 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.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") this.progress = this.el.querySelector("#player-progress")
let enableAudio = () => { let enableAudio = () => {
if(this.player.src){ if(this.player.src){
@ -121,14 +116,13 @@ Hooks.AudioPlayer = {
this.play() this.play()
} }
}) })
this.handleEvent("play", ({url, token, duration, elapsed}) => { this.handleEvent("play", ({url, token, elapsed}) => {
this.playbackBeganAt = nowSeconds() - elapsed this.playbackBeganAt = nowSeconds() - elapsed
let currentSrc = this.player.src.split("?")[0] let currentSrc = this.player.src.split("?")[0]
this.playerDuration = duration
if(currentSrc === url && this.player.paused){ if(currentSrc === url && this.player.paused){
this.play({sync: true}) this.play({sync: true})
} else if(currentSrc !== url) { } else if(currentSrc !== url) {
this.player.src = `${url}?token=${token}&proxy` this.player.src = `${url}?token=${token}`
this.play({sync: true}) this.play({sync: true})
} }
}) })
@ -136,16 +130,16 @@ Hooks.AudioPlayer = {
this.handleEvent("stop", () => this.stop()) this.handleEvent("stop", () => this.stop())
}, },
play(opts = {}){ clearNextTimer(){
console.log("play")
let {sync} = opts
clearInterval(this.progressTimer)
clearTimeout(this.nextTimer) clearTimeout(this.nextTimer)
this.nextTimer = null
},
play(opts = {}){
let {sync} = opts
this.clearNextTimer()
this.player.play().then(() => { this.player.play().then(() => {
if(sync){ if(sync){ this.player.currentTime = nowSeconds() - this.playbackBeganAt }
console.log("sync", nowSeconds() - this.playbackBeganAt)
this.player.currentTime = nowSeconds() - this.playbackBeganAt
}
this.progressTimer = setInterval(() => this.updateProgress(), 100) this.progressTimer = setInterval(() => this.updateProgress(), 100)
}, error => { }, error => {
if(error.name === "NotAllowedError"){ if(error.name === "NotAllowedError"){
@ -156,32 +150,27 @@ Hooks.AudioPlayer = {
pause(){ pause(){
clearInterval(this.progressTimer) clearInterval(this.progressTimer)
clearTimeout(this.nextTimer)
this.player.pause() this.player.pause()
}, },
stop(){ stop(){
clearInterval(this.progressTimer) clearInterval(this.progressTimer)
clearTimeout(this.nextTimer)
this.player.pause() this.player.pause()
this.player.currentTime = 0 this.player.currentTime = 0
this.updateProgress() this.updateProgress()
this.durationText.innerText = "" this.duration.innerText = ""
this.currentTime.innerText = "" this.currentTime.innerText = ""
}, },
updateProgress(){ updateProgress(){
if(this.playerDuration === 0){ return false } if(isNaN(this.player.duration)){ return false }
if(Math.ceil(this.player.currentTime) >= Math.floor(this.playerDuration)){ if(!this.nextTimer && this.player.currentTime >= this.player.duration){
clearInterval(this.progressTimer) clearInterval(this.progressTimer)
this.player.pause() this.nextTimer = setTimeout(() => this.pushEvent("next_song_auto"), rand(0, 1500))
this.playerDuration = 0
console.log("next_song_auto")
this.nextTimer = setTimeout(() => this.pushEvent("next_song_auto"), rand(1000, 3000))
return return
} }
this.progress.style.width = `${(this.player.currentTime / (this.playerDuration) * 100)}%` this.progress.style.width = `${(this.player.currentTime / (this.player.duration) * 100)}%`
this.durationText.innerText = this.formatTime(this.playerDuration) this.duration.innerText = this.formatTime(this.player.duration)
this.currentTime.innerText = this.formatTime(this.player.currentTime) this.currentTime.innerText = this.formatTime(this.player.currentTime)
}, },
@ -226,4 +215,3 @@ liveSocket.connect()
// >> liveSocket.disableLatencySim() // >> liveSocket.disableLatencySim()
window.liveSocket = liveSocket window.liveSocket = liveSocket

View file

@ -8,14 +8,13 @@ defmodule LiveBeatsWeb.FileController do
require Logger 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) path = MediaLibrary.local_filepath(filename_uuid)
mime_type = MIME.from_path(path) mime_type = MIME.from_path(path)
case Phoenix.Token.decrypt(conn, "file", token, max_age: :timer.minutes(1)) do case Phoenix.Token.decrypt(conn, "file", token, max_age: :timer.minutes(1)) do
{:ok, %{vsn: 1, uuid: ^filename_uuid, ip: ip, size: size}} -> {:ok, %{vsn: 1, uuid: ^filename_uuid, ip: ip, size: size}} ->
# if local_file?(filename_uuid, ip) do if local_file?(filename_uuid, ip) do
if !params["proxy"] do
Logger.info("serving file from #{server_ip()}") Logger.info("serving file from #{server_ip()}")
do_send_file(conn, path) do_send_file(conn, path)
else else
@ -42,8 +41,7 @@ defmodule LiveBeatsWeb.FileController do
defp proxy_file(conn, ip, mime_type, content_length) do defp proxy_file(conn, ip, mime_type, content_length) do
uri = conn |> request_url() |> URI.parse() uri = conn |> request_url() |> URI.parse()
port = LiveBeatsWeb.Endpoint.config(:http)[:port] port = LiveBeatsWeb.Endpoint.config(:http)[:port]
# path = uri.path <> "?" <> uri.query <> "&from=#{server_ip()}" path = uri.path <> "?" <> uri.query <> "&from=#{server_ip()}"
path = uri.path <> "?" <> String.replace(uri.query, "&proxy", "") <> "&from=#{server_ip()}"
{:ok, ipv6} = :inet.parse_address(String.to_charlist(ip)) {:ok, ipv6} = :inet.parse_address(String.to_charlist(ip))
{:ok, req} = Mint.HTTP.connect(:http, ipv6, port, file_server_opts()) {:ok, req} = Mint.HTTP.connect(:http, ipv6, port, file_server_opts())
{:ok, req, request_ref} = Mint.HTTP.request(req, "GET", path, [], "") {:ok, req, request_ref} = Mint.HTTP.request(req, "GET", path, [], "")
@ -51,7 +49,7 @@ defmodule LiveBeatsWeb.FileController do
conn conn
|> put_resp_header("content-type", mime_type) |> put_resp_header("content-type", mime_type)
|> put_resp_header("accept-ranges", "bytes") |> 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) |> send_chunked(200)
|> stream(req, request_ref) |> stream(req, request_ref)
end end