Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial support for media session api #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions playlist-mini.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,64 @@ function evaluateStatus(data) {
if (document.getElementById('artist_mini').innerHTML.indexOf('tileSizePHP') < 0){
changeTileSizeInfo();
}

updateMediaSession(data);
}

function updateMediaSession(data) {
navigator.mediaSession.playbackState = data.isplaying == 1 ? "playing" : "paused"
}

function getArtworkSource(data) {
if (data.album_id) {
if (data.thumbnail) {
//temporary solution for HRA streams
return "image_crop.php?thumbnail=" + encodeURIComponent(data.thumbnail);
}
else {
return "image.php?image_id=" + data.image_id + "&quality=hq&track_id=" + data.track_id;
}
}
else if (data.thumbnail) {
return "image_crop.php?thumbnail=" + encodeURIComponent(data.thumbnail);
}
else if (data.imageFile) {
return data.imageFile;
}
else {
return "image/large_file_not_found.png";
}
}

function evaluateMediaSession(data) {

if (!this.audioTag) {
this.audioTag = document.createElement('audio');
document.body.appendChild(this.audioTag);
this.audioTag.src = "https://raw.githubusercontent.com/anars/blank-audio/master/10-seconds-of-silence.mp3";
this.audioTag.loop = true;
}

// data.artist, data.title, data.album, data.by, data.album_id, data.image_id
if (this.audioTag) {
navigator.mediaSession.metadata = new MediaMetadata({
title: data.title,
artist: data.track_artist_all,
album: data.album,
artwork: [{ src: getArtworkSource(data) }]
});

navigator.mediaSession.setActionHandler('previoustrack', () => ajaxRequest('play.php?action=prev&amp;menu=playlist'));
navigator.mediaSession.setActionHandler('nexttrack', () => ajaxRequest('play.php?action=next&amp;menu=playlist'));
navigator.mediaSession.setActionHandler('play', async () => {
ajaxRequest('play.php?action=play&amp;menu=playlist')
navigator.mediaSession.playbackState = 'playing';
});
navigator.mediaSession.setActionHandler('pause', () => {
ajaxRequest('play.php?action=pause&amp;menu=playlist')
navigator.mediaSession.playbackState = 'paused';
});
}
}


Expand All @@ -182,6 +240,10 @@ function evaluateIsplaying(isplaying, idx) {
isplaying = isplaying.state;
}
if (isplaying == 0) {
if (this.audioTag) {
this.audioTag.pause();
this.audioTagcurrentTime = 0;
}
// stop
$("#time").removeClass();
$("#time").addClass("icon-anchor");
Expand All @@ -192,6 +254,9 @@ function evaluateIsplaying(isplaying, idx) {
previous_miliseconds = 0;
}
else if (isplaying == 1) {
if (this.audioTag) {
this.audioTag.play();
}
// play
$("#time").removeClass();
$("#time").addClass("icon-anchor");
Expand All @@ -201,6 +266,9 @@ function evaluateIsplaying(isplaying, idx) {
$("#play").attr("onclick","javascript:ajaxRequest('play.php?action=pause&menu=playlist', evaluateIsplaying);");
}
else if (isplaying == 3) {
if (this.audioTag) {
this.audioTag.pause();
}
// pause
$("#time").removeClass();
$("#time").addClass("blink_me icon-anchor");
Expand Down Expand Up @@ -351,6 +419,8 @@ function evaluateTrack(data) {
else {
document.getElementById('image_container_mini').innerHTML = '<a href="playlist.php"><img id="image_in_mini" src="<?php echo 'image/'; ?>large_file_not_found.png" alt=""></a>';
}

evaluateMediaSession(data);
}

function evaluatePlaytime(data) {
Expand Down
103 changes: 98 additions & 5 deletions playlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,14 @@ function evaluateStatus (data) {
<div class="pl-fld-name">year</div>

<!-- <div class="pl-fld-name">file info</div> -->
<div class="pl-track-artist"><span id="lyrics">&nbsp;</span></div>
<div id="lyrics_block">
<div class="pl-track-artist"><span id="lyrics">&nbsp;</span></div>
<div class="pl-fld-name">search</div>

</div>
<div id="favorites_block">
<div class="pl-track-favorites"><span id="favorites">&nbsp;</span></div>
<div class="pl-fld-name">favorites/blacklist</div>

</div>

</div>

Expand Down Expand Up @@ -651,9 +653,13 @@ function initialize() {
ajaxRequest('play.php?action=playlistTrack&track_id=' + track_id[<?php echo $listpos; ?>] + '&menu=playlist', evaluateTrack);
ajaxRequest('play.php?action=playlistStatus&track_id=' + track_id[<?php echo $listpos; ?>] + '&menu=playlist', evaluateStatus);
//ajaxRequest('ajax-track-version.php?track_id=' + track_id[<?php echo $listpos; ?>] + '&menu=playlist', evaluateTrackVersion);


}




function evaluateStatus(data) {

// data.hash, data.miliseconds, data.listpos, data.volume
Expand Down Expand Up @@ -684,7 +690,7 @@ function evaluateStatus(data) {
} */
var rel_file = encodeURIComponent(data.relative_file);
//console.log ("rel_file=" + rel_file);
var params = data.audio_dataformat + '&nbsp;&bull;&nbsp;' + data.audio_bits_per_sample + '&nbsp;bit - ' + data.audio_sample_rate/1000 + '&nbsp;kHz&nbsp;&bull;&nbsp;<div style="width: 6em; display: inline-flex;">' + data.audio_profile + '</div>';
var params = data.stream_source + data.audio_dataformat + '&nbsp;&bull;&nbsp;' + data.audio_bits_per_sample + '&nbsp;bit - ' + data.audio_sample_rate/1000 + '&nbsp;kHz&nbsp;&bull;&nbsp;<div style="width: 6em; display: inline-flex;">' + data.audio_profile + '</div>';
document.getElementById('parameters').innerHTML = params;


Expand All @@ -708,6 +714,15 @@ function evaluateStatus(data) {
else {
$("#saveCurrentTrack").show();
}

//move fav/blklist star when it overlaps time bar (long track/album title, etc)
var favBottom = $('#favorites_block').position().top + $('#favorites_block').outerHeight(true);
var controlTop = $('.media_control').position().top;
//console.log('bott=' + favBottom + ' top=' + controlTop);
if (favBottom > (controlTop + 7)) {
$('#lyrics_block').css("display","inline-block");
}

evaluateListpos(data.listpos);
evaluatePlaytime(data);
evaluateRepeat(data.repeat);
Expand All @@ -716,8 +731,72 @@ function evaluateStatus(data) {
evaluateVolume(data.volume);
evaluateGain(data.gain);
evaluateConsume(data);
updateMediaSession(data);
}

function updateMediaSession(data) {
navigator.mediaSession.playbackState = data.isplaying == 1 ? "playing" : "paused"

if (data.isplaying == 1) {
navigator.mediaSession.setPositionState({
duration: data.max/1000,
playbackRate: 1,
position: data.miliseconds/1000
});
}
}

function getArtworkSource(data) {
if (data.album_id) {
if (data.thumbnail) {
//temporary solution for HRA streams
return "image_crop.php?thumbnail=" + encodeURIComponent(data.thumbnail);
}
else {
return "image.php?image_id=" + data.image_id + "&quality=hq&track_id=" + data.track_id;
}
}
else if (data.thumbnail) {
return "image_crop.php?thumbnail=" + encodeURIComponent(data.thumbnail);
}
else if (data.imageFile) {
return data.imageFile;
}
else {
return "image/large_file_not_found.png";
}
}

function evaluateMediaSession(data) {

if (!this.audioTag) {
this.audioTag = document.createElement('audio');
document.body.appendChild(this.audioTag);
this.audioTag.src = "https://raw.githubusercontent.com/anars/blank-audio/master/10-seconds-of-silence.mp3";
this.audioTag.loop = true;
}

// data.artist, data.title, data.album, data.by, data.album_id, data.image_id
if (this.audioTag) {
navigator.mediaSession.metadata = new MediaMetadata({
title: data.title,
artist: data.track_artist_all,
album: data.album,
artwork: [{ src: getArtworkSource(data) }]
});

navigator.mediaSession.setActionHandler('previoustrack', () => ajaxRequest('play.php?action=prev&amp;menu=playlist'));
navigator.mediaSession.setActionHandler('nexttrack', () => ajaxRequest('play.php?action=next&amp;menu=playlist'));
navigator.mediaSession.setActionHandler('play', async () => {
ajaxRequest('play.php?action=play&amp;menu=playlist')
navigator.mediaSession.playbackState = 'playing';
});
navigator.mediaSession.setActionHandler('pause', () => {
ajaxRequest('play.php?action=pause&amp;menu=playlist')
navigator.mediaSession.playbackState = 'paused';
});
}
}

function evaluateListpos(listpos) {
if (previous_listpos != listpos) {
Expand Down Expand Up @@ -830,7 +909,12 @@ function evaluateIsplaying(isplaying, idx) {
idx = isplaying.idx;
isplaying = isplaying.state;
}

if (isplaying == 0) {
if (this.audioTag) {
this.audioTag.pause();
this.audioTagcurrentTime = 0;
}
// stop
$("#time").removeClass();
$("#time").addClass("icon-anchor");
Expand All @@ -845,6 +929,9 @@ function evaluateIsplaying(isplaying, idx) {
previous_miliseconds = 0;
}
else if (isplaying == 1) {
if (this.audioTag) {
this.audioTag.play();
}
// play
document.getElementById('track' + idx + '_play').style.visibility = 'visible';
$("#time").removeClass();
Expand All @@ -857,6 +944,9 @@ function evaluateIsplaying(isplaying, idx) {
//$('#track' + idx + '_play').show();
}
else if (isplaying == 3) {
if (this.audioTag) {
this.audioTag.pause();
}
// pause
$("#time").removeClass();
$("#time").addClass("blink_me icon-anchor");
Expand Down Expand Up @@ -986,6 +1076,7 @@ function evaluateTrack(data) {
$('#title1_wait_indicator').hide();
$('#title_wait_indicator').hide();
$('#fileInfoForDbTracks').css('visibility', 'visible');
$('#lyrics_block').css("display","block");
current_track_id = data.track_id;
current_track_mpd_url = data.track_mpd_url;
if (previous_track_id != data.track_id && data.track_id != null) {
Expand Down Expand Up @@ -1267,7 +1358,9 @@ function evaluateTrack(data) {
changeTileSizeInfo();
resizeImgContainer();
getFavoritesList(current_track_id,current_track_mpd_url);



evaluateMediaSession(data);
/* spinnerImg.stop();
$('#image').css('position', 'relative');*/
//$("#waitIndicatorImg").hide();
Expand Down