mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-21 23:50:59 +00:00
Fix seekable songs
This commit is contained in:
parent
214ec50f0e
commit
eecb14ecba
2 changed files with 21 additions and 35 deletions
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue