native.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Native.js
  3. This script is used to match the Music Player to as native as possible
  4. for some advanced use case (like PWA)
  5. */
  6. //Native Media Player
  7. function initNativeMediaPlayer(){
  8. var skipTime = 10;
  9. if ('mediaSession' in navigator) {
  10. navigator.mediaSession.setActionHandler('play', function() {
  11. gainAudio();
  12. setPlaying(true);
  13. });
  14. navigator.mediaSession.setActionHandler('pause', function() {
  15. originalVol = audioElement[0].volume;
  16. fadeAudio();
  17. setPlaying(false);
  18. });
  19. navigator.mediaSession.setActionHandler('stop', function() {
  20. originalVol = audioElement[0].volume;
  21. fadeAudio();
  22. setPlaying(false);
  23. audioElement[0].pause();
  24. setTimeout(function(){
  25. audioElement[0].volume = defaultVolumeBeforeFadeout;
  26. },500);
  27. });
  28. navigator.mediaSession.setActionHandler('seekbackward', function() {
  29. audioElementObject.currentTime = Math.max(audioElementObject.currentTime - skipTime, 0);
  30. setTimeout(function(){
  31. updatePositionState();
  32. }, 500);
  33. });
  34. navigator.mediaSession.setActionHandler('seekforward', function() {
  35. audioElementObject.currentTime = Math.min(audioElementObject.currentTime + skipTime, audioElementObject.duration);
  36. setTimeout(function(){
  37. updatePositionState();
  38. }, 500);
  39. });
  40. navigator.mediaSession.setActionHandler('seekto', function(evt) {
  41. if (evt.fastSeek && ('fastSeek' in audioElementObject)) {
  42. audioElementObject.fastSeek(evt.seekTime);
  43. return;
  44. }
  45. audioElementObject.currentTime = evt.seekTime;
  46. setTimeout(function(){
  47. updatePositionState();
  48. }, 500);
  49. });
  50. navigator.mediaSession.setActionHandler('previoustrack', function() {
  51. previousSong();
  52. });
  53. navigator.mediaSession.setActionHandler('nexttrack', function() {
  54. nextSong();
  55. });
  56. }
  57. }
  58. function updateTitle(title, artist, albumn, filepath=""){
  59. if ('mediaSession' in navigator) {
  60. if (isAndroid && navigator.mediaSession.metadata){
  61. //Media Session created. Update the existsing one instead
  62. navigator.mediaSession.metadata.title = title;
  63. navigator.mediaSession.metadata.artist = artist;
  64. navigator.mediaSession.metadata.album = albumn;
  65. }else{
  66. let nohashURL = window.location.hash.split("#")[0];
  67. navigator.mediaSession.metadata = new MediaMetadata({
  68. title: title,
  69. artist: artist,
  70. album: albumn,
  71. /* artwork: [
  72. { src: nohashURL + "img/default.png"}
  73. ]
  74. */
  75. });
  76. }
  77. }
  78. }
  79. function updatePositionState(currentTime, duration) {
  80. if (isNaN(currentTime) || isNaN(duration)){
  81. return;
  82. }
  83. if ('setPositionState' in navigator.mediaSession) {
  84. navigator.mediaSession.setPositionState({
  85. duration: audioElement[0].duration,
  86. playbackRate: audioElement[0].playbackRate,
  87. position: audioElement[0].currentTime
  88. });
  89. }
  90. }