-
Notifications
You must be signed in to change notification settings - Fork 18
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
implement mediaSession controls for better android integration #1
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -232,7 +232,6 @@ function changeSong(index, noAutoplay = false) { | |
} else { | ||
document.getElementById('btnPrevious').removeAttribute('disabled'); | ||
} | ||
|
||
pausePlayer(true); | ||
const hash = MODEL + '_' + allData[index].fileName; | ||
window.location.hash = hash; | ||
|
@@ -480,3 +479,44 @@ function sketch(p) { | |
p.rect(x, y, w, h); | ||
} | ||
}; | ||
|
||
if (navigator.mediaSession) { | ||
|
||
function playAudio() { | ||
var audio = document.createElement('audio'); | ||
audio.src = './silence.mp3'; // silence must be at least 5 seconds https://freesound.org/people/silentspeaker/sounds/426895/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand what this is and why it's needed. Do you have a link to a reference or the spec? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://developers.google.com/web/updates/2017/02/media-session#implementation_notes see point 5. specifically
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, then can you leave that sentence and the link as a comment, please? |
||
|
||
// to keep the controls open, must loop the silence | ||
audio.addEventListener('ended', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.currentTime = 0; | ||
this.play(); | ||
}, false); | ||
|
||
audio.play().then(_ => { | ||
let playing = true; | ||
navigator.mediaSession.setActionHandler('play', function() { | ||
audio.play() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. semicolons everywhere, |
||
}); | ||
navigator.mediaSession.setActionHandler('pause', function() { | ||
if (playing) { | ||
audio.pause() | ||
window.pausePlayer() | ||
} | ||
else { | ||
audio.play() | ||
window.startPlayer() | ||
} | ||
playing = !playing | ||
}); | ||
navigator.mediaSession.setActionHandler('nexttrack', function() { | ||
window.nextSong() | ||
//store.dispatch(next()); | ||
}); | ||
}).catch(error => { console.log(error); }); | ||
} | ||
document.addEventListener('DOMContentLoaded', function () { | ||
document.querySelectorAll('.interact').forEach(b => { | ||
b.addEventListener('click', playAudio) | ||
}) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh good catch. I have added to the pr for all the good style catches. incoming shortly