-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from McDic/feature/soundcloud-music-player
Add Soundcloud music player
- Loading branch information
Showing
9 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
function getSC() { | ||
var musicplayerElement = document.querySelector("iframe.soundcloud"); | ||
if (musicplayerElement == null) { | ||
return null; | ||
} | ||
return SC.Widget(musicplayerElement); | ||
} | ||
|
||
async function musicplayerOnLoad() { | ||
|
||
// Private class for storing nonlocal static variables of music player | ||
class MusicPlayerStaticVariables { | ||
constructor(sc_widget) { | ||
this.sc = sc_widget; | ||
this.playlistLength = 1; | ||
this.isPlaying = false; | ||
this.ok_move = false; | ||
} | ||
|
||
updateByGetters() { | ||
this.ok_move = false; | ||
this.sc.getSounds((sounds) => { | ||
this.playlistLength = sounds.length; | ||
this.sc.isPaused((paused) => { | ||
this.isPlaying = !paused; | ||
this.ok_move = true; | ||
}) | ||
}); | ||
} | ||
}; | ||
|
||
// Private function returning random integer in range [a, b) | ||
function randomInt(a, b) { | ||
return Math.floor(Math.random() * (b - a)) + a; | ||
} | ||
|
||
// Private function waiting for condition to be true | ||
async function whileWaiting(condition_callback, action_callback, timeout=200) { | ||
while (!condition_callback()) { | ||
await new Promise(resolve => setTimeout(resolve, timeout)); | ||
if (action_callback != null) action_callback(); | ||
} | ||
} | ||
|
||
var widget = getSC(); | ||
await whileWaiting(() => (widget != null), () => { widget = getSC(); }); | ||
var controller_button = document.querySelector("header.md-header .music-player.mcdic"); | ||
var musicState = new MusicPlayerStaticVariables(widget); | ||
playButtonMode(); | ||
|
||
function playButtonMode() { | ||
controller_button.classList.remove("pause-button"); | ||
controller_button.classList.add("play-button"); | ||
controller_button.title = "Play random music"; | ||
} | ||
|
||
function stopButtonMode() { | ||
controller_button.classList.remove("play-button"); | ||
controller_button.classList.add("pause-button"); | ||
controller_button.title = "Stop this music"; | ||
} | ||
|
||
async function togglePlay() { | ||
musicState.updateByGetters(); | ||
await whileWaiting(() => musicState.ok_move, null); | ||
if (musicState.isPlaying) { | ||
widget.pause(); | ||
playButtonMode(); | ||
} else { | ||
widget.skip(randomInt(0, musicState.playlistLength)); | ||
widget.seekTo(0); | ||
widget.setVolume(25); | ||
widget.play(); | ||
stopButtonMode(); | ||
} | ||
} | ||
controller_button.addEventListener("click", () => togglePlay().then(null)); | ||
} | ||
|
||
musicplayerOnLoad().then(null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div class="music-player mcdic play-button"> | ||
</div> | ||
<iframe | ||
class="soundcloud" title="soundcloud" | ||
width="0px" height="0px" scrolling="no" frameborder="no" allow="autoplay" | ||
src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/1952629623&color=%2328b928&auto_play=false&hide_related=true&show_comments=false&show_user=true&show_reposts=false&show_teaser=true"> | ||
</iframe> |