1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /*
- Native.js
- This script is used to match the Music Player to as native as possible
- for some advanced use case (like PWA)
- */
- //Native Media Player
- function initNativeMediaPlayer(){
- var skipTime = 10;
- if ('mediaSession' in navigator) {
- navigator.mediaSession.setActionHandler('play', function() {
- gainAudio();
- setPlaying(true);
- });
- navigator.mediaSession.setActionHandler('pause', function() {
- originalVol = audioElement[0].volume;
- fadeAudio();
- setPlaying(false);
- });
- navigator.mediaSession.setActionHandler('stop', function() {
- originalVol = audioElement[0].volume;
- fadeAudio();
- setPlaying(false);
- audioElement[0].pause();
- setTimeout(function(){
- audioElement[0].volume = defaultVolumeBeforeFadeout;
- },500);
- });
- navigator.mediaSession.setActionHandler('seekbackward', function() {
- audioElementObject.currentTime = Math.max(audioElementObject.currentTime - skipTime, 0);
- setTimeout(function(){
- updatePositionState();
- }, 500);
- });
- navigator.mediaSession.setActionHandler('seekforward', function() {
- audioElementObject.currentTime = Math.min(audioElementObject.currentTime + skipTime, audioElementObject.duration);
- setTimeout(function(){
- updatePositionState();
- }, 500);
- });
- navigator.mediaSession.setActionHandler('seekto', function(evt) {
- if (evt.fastSeek && ('fastSeek' in audioElementObject)) {
- audioElementObject.fastSeek(evt.seekTime);
- return;
- }
- audioElementObject.currentTime = evt.seekTime;
- setTimeout(function(){
- updatePositionState();
- }, 500);
- });
- navigator.mediaSession.setActionHandler('previoustrack', function() {
- previousSong();
- });
- navigator.mediaSession.setActionHandler('nexttrack', function() {
- nextSong();
- });
- }
- }
- function updateTitle(title, artist, albumn, filepath=""){
- if ('mediaSession' in navigator) {
- if (isAndroid && navigator.mediaSession.metadata){
- //Media Session created. Update the existsing one instead
- navigator.mediaSession.metadata.title = title;
- navigator.mediaSession.metadata.artist = artist;
- navigator.mediaSession.metadata.album = albumn;
- }else{
- let nohashURL = window.location.hash.split("#")[0];
- navigator.mediaSession.metadata = new MediaMetadata({
- title: title,
- artist: artist,
- album: albumn,
- /* artwork: [
- { src: nohashURL + "img/default.png"}
- ]
- */
- });
- }
- }
- }
- function updatePositionState(currentTime, duration) {
- if (isNaN(currentTime) || isNaN(duration)){
- return;
- }
- if ('setPositionState' in navigator.mediaSession) {
- navigator.mediaSession.setPositionState({
- duration: audioElement[0].duration,
- playbackRate: audioElement[0].playbackRate,
- position: audioElement[0].currentTime
- });
- }
- }
|