Fixing music.mpd.js both for mpd and mopidy backends

This commit is contained in:
Fabio Manganiello 2019-02-13 00:27:36 +01:00
parent f0f6d43990
commit 3190d3c6c8
2 changed files with 28 additions and 22 deletions

View file

@ -46,16 +46,25 @@ $(document).ready(function() {
var elapsed; var length; var elapsed; var length;
var timeToStr = (time) => { var timeToStr = (time) => {
if (typeof time === 'string') {
return time;
}
return parseInt(parseInt(time)/60) + ':' return parseInt(parseInt(time)/60) + ':'
+ (parseInt(time)%60 < 10 ? '0' : '') + (parseInt(time)%60); + (parseInt(time)%60 < 10 ? '0' : '') + (parseInt(time)%60);
}; };
if (status) { if (status) {
if (seekInterval) { if ('position' in status) {
clearInterval(seekInterval); elapsed = parseInt(status.position);
$trackSeeker.val(elapsed);
} }
if ('time' in status) { if ('time' in status) {
if (seekInterval) {
clearInterval(seekInterval);
}
if (typeof status.time === 'string' && status.time.indexOf(':') > -1) { if (typeof status.time === 'string' && status.time.indexOf(':') > -1) {
// backend.music.mpd time format: "elapsed:total" // backend.music.mpd time format: "elapsed:total"
[elapsed, length] = status.time.split(':'); [elapsed, length] = status.time.split(':');
@ -65,22 +74,18 @@ $(document).ready(function() {
// backend.music.mopidy time format: integer with elapsed seconds // backend.music.mopidy time format: integer with elapsed seconds
length = parseInt(status.time); length = parseInt(status.time);
} }
if (length) {
$trackSeeker.val(length);
}
if (elapsed) {
$trackSeeker.attr('max', elapsed);
curTrackElapsed = {
'timestamp': new Date().getTime(),
'elapsed': elapsed,
};
}
} }
if ('position' in status) { if (elapsed !== undefined) {
$trackSeeker.val(parseInt(status.position)); $trackSeeker.val(elapsed);
curTrackElapsed = {
'timestamp': new Date().getTime(),
'elapsed': elapsed,
};
}
if (length !== undefined) {
$trackSeeker.attr('max', length);
} }
if ('state' in status) { if ('state' in status) {
@ -104,8 +109,8 @@ $(document).ready(function() {
$curTrack.find('.no-track').hide(); $curTrack.find('.no-track').hide();
$trackSeeker.prop('disabled', false); $trackSeeker.prop('disabled', false);
$('#seek-time-elapsed').text(elapsed ? elapsed : '-:--'); $('#seek-time-elapsed').text(elapsed ? timeToStr(elapsed) : '-:--');
$('#seek-time-length').text(length ? length : '-:--'); $('#seek-time-length').text(length ? timeToStr(length) : '-:--');
break; break;
case 'play': case 'play':
@ -116,8 +121,8 @@ $(document).ready(function() {
$curTrack.find('.no-track').hide(); $curTrack.find('.no-track').hide();
$trackSeeker.prop('disabled', false); $trackSeeker.prop('disabled', false);
$('#seek-time-elapsed').text(elapsed ? elapsed : '-:--'); $('#seek-time-elapsed').text(elapsed ? timeToStr(elapsed) : '-:--');
$('#seek-time-length').text(length ? length : '-:--'); $('#seek-time-length').text(length ? timeToStr(length) : '-:--');
seekInterval = setInterval(function() { seekInterval = setInterval(function() {
var length = parseInt($trackSeeker.attr('max')); var length = parseInt($trackSeeker.attr('max'));
@ -126,8 +131,8 @@ $(document).ready(function() {
if (value < length) { if (value < length) {
$trackSeeker.val(value); $trackSeeker.val(value);
elapsed = parseInt(value/60) + ':' + (value%60 < 10 ? '0' : '') + (value%60); elapsed = value;
$('#seek-time-elapsed').text(elapsed); $('#seek-time-elapsed').text(timeToStr(elapsed));
} }
}, 1000); }, 1000);
break; break;

View file

@ -151,6 +151,7 @@ class MusicMopidyBackend(Backend):
track['artist'] = m.group(1) track['artist'] = m.group(1)
track['title'] = m.group(2) track['title'] = m.group(2)
track['position'] = 0.0
self.bus.post(NewPlayingTrackEvent(status=status, track=track)) self.bus.post(NewPlayingTrackEvent(status=status, track=track))
elif event == 'volume_changed': elif event == 'volume_changed':
status['volume'] = msg.get('volume') status['volume'] = msg.get('volume')