Skip to content

Commit

Permalink
Merge pull request #185 from kenellorando/background-load-fix
Browse files Browse the repository at this point in the history
Fix continuous/non-continuous background load functionality
  • Loading branch information
kenellorando authored Dec 1, 2020
2 parents 05a8e60 + e425103 commit 6411b24
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<main>
<div id="player">
<!-- Connection to source -->
<audio id="stream" src="https://stream.cadenceradio.com/cadence1" type="audio/mp3"></audio>
<audio id="stream" src="" type="audio/mp3"></audio>
<!-- Now playing information -->
<div id="status">Disconnected from server.</div>

Expand Down
20 changes: 11 additions & 9 deletions public/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,26 @@ function radioTitle() {

// Toggle the stream with the playButton
$(document).ready(function () {
document.getElementById("playButton").addEventListener('click', function(){
var stream = document.getElementById("stream");
var mobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);
var stream = document.getElementById("stream");
var mobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);

if (!mobile) {
stream.src = "https://stream.cadenceradio.com/cadence1";
stream.load();
}

// Here, we pause and play the stream
// If the device is mobile, we remove the stream source entirely
// so music data stops loading in the background.
document.getElementById("playButton").addEventListener('click', function(){
if (stream.paused) {
// Reload the audio source if on mobile
// Reload the audio source
if (mobile) {
stream.src = "https://stream.cadenceradio.com/cadence1";
stream.load();
}
stream.load();
stream.play();
// Replace the ❙❙ in the button when playing
document.getElementById("playButton").innerHTML = "❙❙";
} else {
// If mobile, clear the audio source
// Clear the audio source
if (mobile) {
stream.src = "";
}
Expand Down

0 comments on commit 6411b24

Please sign in to comment.