audio player: add pause-fade + track-restart +

fix ogvjs paused-seek
This commit is contained in:
ed 2021-06-25 01:46:30 +02:00
parent ea38b8041a
commit 9366512f2f

View file

@ -345,14 +345,15 @@ var mpl = (function () {
// extract songs + add play column // extract songs + add play column
function MPlayer() { function MPlayer() {
this.id = Date.now(); var r = this;
this.au = null; r.id = Date.now();
this.au_native = null; r.au = null;
this.au_native2 = null; r.au_native = null;
this.au_ogvjs = null; r.au_native2 = null;
this.au_ogvjs2 = null; r.au_ogvjs = null;
this.tracks = {}; r.au_ogvjs2 = null;
this.order = []; r.tracks = {};
r.order = [];
var re_audio = /\.(opus|ogg|m4a|aac|mp3|wav|flac)$/i, var re_audio = /\.(opus|ogg|m4a|aac|mp3|wav|flac)$/i,
trs = QSA('#files tbody tr'); trs = QSA('#files tbody tr');
@ -367,32 +368,33 @@ function MPlayer() {
if (m) { if (m) {
var tid = link.getAttribute('id'); var tid = link.getAttribute('id');
this.order.push(tid); r.order.push(tid);
this.tracks[tid] = url; r.tracks[tid] = url;
tds[0].innerHTML = '<a id="a' + tid + '" href="#a' + tid + '" class="play">play</a></td>'; tds[0].innerHTML = '<a id="a' + tid + '" href="#a' + tid + '" class="play">play</a></td>';
ebi('a' + tid).onclick = ev_play; ebi('a' + tid).onclick = ev_play;
} }
} }
this.vol = sread('vol'); r.vol = sread('vol');
if (this.vol !== null) if (r.vol !== null)
this.vol = parseFloat(this.vol); r.vol = parseFloat(r.vol);
else else
this.vol = 0.5; r.vol = 0.5;
this.expvol = function () { r.expvol = function (v) {
return 0.5 * this.vol + 0.5 * this.vol * this.vol; return 0.5 * v + 0.5 * v * v;
}; };
this.setvol = function (vol) { r.setvol = function (vol) {
this.vol = Math.max(Math.min(vol, 1), 0); r.vol = Math.max(Math.min(vol, 1), 0);
swrite('vol', vol); swrite('vol', vol);
r.stopfade(true);
if (this.au) if (r.au)
this.au.volume = this.expvol(); r.au.volume = r.expvol(r.vol);
}; };
this.read_order = function () { r.read_order = function () {
var order = [], var order = [],
links = QSA('#files>tbody>tr>td:nth-child(1)>a'); links = QSA('#files>tbody>tr>td:nth-child(1)>a');
@ -403,24 +405,69 @@ function MPlayer() {
order.push(tid.slice(1)); order.push(tid.slice(1));
} }
this.order = order; r.order = order;
}; };
this.preload = function (url) { r.fdir = 0;
r.fvol = -1;
r.ftid = -1;
r.ftimer = null;
r.fade_in = function () {
r.fvol = 0;
r.fdir = 0.025;
if (r.au) {
r.ftid = r.au.tid;
r.au.play();
fader();
}
};
r.fade_out = function () {
r.fvol = r.vol;
r.fdir = -0.05;
r.ftid = r.au.tid;
fader();
};
r.stopfade = function (hard) {
clearTimeout(r.ftimer);
if (hard)
r.ftid = -1;
}
function fader() {
r.stopfade();
if (!r.au || r.au.tid !== r.ftid)
return;
var done = true;
r.fvol += r.fdir;
if (r.fvol < 0) {
r.fvol = 0;
r.au.pause();
}
else if (r.fvol > r.vol)
r.fvol = r.vol;
else
done = false;
r.au.volume = r.expvol(r.fvol);
if (!done)
setTimeout(fader, 10);
}
r.preload = function (url) {
var au = null; var au = null;
if (need_ogv_for(url)) { if (need_ogv_for(url)) {
au = mp.au_ogvjs2; au = mp.au_ogvjs2;
if (!au && window['OGVPlayer']) { if (!au && window['OGVPlayer']) {
au = new OGVPlayer(); au = new OGVPlayer();
au.preload = "auto"; au.preload = "auto";
this.au_ogvjs2 = au; r.au_ogvjs2 = au;
} }
} else { } else {
au = mp.au_native2; au = mp.au_native2;
if (!au) { if (!au) {
au = new Audio(); au = new Audio();
au.preload = "auto"; au.preload = "auto";
this.au_native2 = au; r.au_native2 = au;
} }
} }
if (au) { if (au) {
@ -735,9 +782,8 @@ function seek_au_sec(seek) {
mp.au.currentTime = seek; mp.au.currentTime = seek;
// ogv.js breaks on .play() during playback if (mp.au.paused)
if (mp.au === mp.au_native) mp.fade_in();
mp.au.play();
mpui.progress_updater(); mpui.progress_updater();
} }
@ -759,6 +805,10 @@ function next_song(e) {
} }
function prev_song(e) { function prev_song(e) {
ev(e); ev(e);
if (mp.au && !mp.au.paused && mp.au.currentTime > 3)
return seek_au_sec(0);
return song_skip(-1); return song_skip(-1);
} }
@ -767,9 +817,9 @@ function playpause(e) {
ev(e); ev(e);
if (mp.au) { if (mp.au) {
if (mp.au.paused) if (mp.au.paused)
mp.au.play(); mp.fade_in();
else else
mp.au.pause(); mp.fade_out();
mpui.progress_updater(); mpui.progress_updater();
} }
@ -788,7 +838,8 @@ function playpause(e) {
ebi('bnext').onclick = next_song; ebi('bnext').onclick = next_song;
ebi('barpos').onclick = function (e) { ebi('barpos').onclick = function (e) {
if (!mp.au) { if (!mp.au) {
return play(0); play(0);
return mp.fade_in();
} }
var rect = pbar.buf.can.getBoundingClientRect(), var rect = pbar.buf.can.getBoundingClientRect(),
@ -863,7 +914,12 @@ var mpui = (function () {
// event from play button next to a file in the list // event from play button next to a file in the list
function ev_play(e) { function ev_play(e) {
ev(e); ev(e);
var fade = !mp.au || mp.au.paused;
play(this.getAttribute('id').slice(1)); play(this.getAttribute('id').slice(1));
if (fade)
mp.fade_in();
return false; return false;
} }
@ -1106,6 +1162,8 @@ function play(tid, seek, call_depth) {
if (mp.order.length == 0) if (mp.order.length == 0)
return console.log('no audio found wait what'); return console.log('no audio found wait what');
mp.stopfade(true);
var tn = tid; var tn = tid;
if ((tn + '').indexOf('f-') === 0) if ((tn + '').indexOf('f-') === 0)
tn = mp.order.indexOf(tn); tn = mp.order.indexOf(tn);
@ -1181,7 +1239,7 @@ function play(tid, seek, call_depth) {
mp.au.tid = tid; mp.au.tid = tid;
mp.au.src = url + (url.indexOf('?') < 0 ? '?cache' : '&cache'); mp.au.src = url + (url.indexOf('?') < 0 ? '?cache' : '&cache');
mp.au.volume = mp.expvol(); mp.au.volume = mp.expvol(mp.vol);
var oid = 'a' + tid; var oid = 'a' + tid;
setclass(oid, 'play act'); setclass(oid, 'play act');
var trs = ebi('files').getElementsByTagName('tbody')[0].getElementsByTagName('tr'); var trs = ebi('files').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
@ -1300,6 +1358,7 @@ function autoplay_blocked(seek) {
// depending on win10 settings or something? idk // depending on win10 settings or something? idk
mp.au_native = mp.au_ogvjs = null; mp.au_native = mp.au_ogvjs = null;
play(tid, seek); play(tid, seek);
mp.fade_in();
}; };
na.onclick = unblocked; na.onclick = unblocked;
} }
@ -1611,9 +1670,11 @@ document.onkeydown = function (e) {
if (pos !== -1) if (pos !== -1)
return seek_au_mul(pos) || true; return seek_au_mul(pos) || true;
var n = k == 'KeyJ' ? -1 : k == 'KeyL' ? 1 : 0; if (k == 'KeyJ')
if (n !== 0) return prev_song() || true;
return song_skip(n) || true;
if (k == 'KeyL')
return next_song() || true;
if (k == 'KeyP') if (k == 'KeyP')
return playpause() || true; return playpause() || true;