Last active
November 18, 2015 05:24
-
-
Save haydenbleasel/975f55a9a0240ceb251f to your computer and use it in GitHub Desktop.
jQuery setup for the SoundCloud API
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
$ -> | |
# Create abc,def string | |
Number::numberWithCommas = | |
String::numberWithCommas = -> | |
@toString().replace /\B(?=(\d{3})+(?!\d))/g, ',' | |
# Initial sound is empty | |
sound = undefined | |
# Pause the current song | |
pause = -> | |
if (sound) | |
sound.pause() | |
$('#pause').hide 0 | |
$('#play').show 0 | |
# Play the song ID | |
play = (id) -> | |
$('#play').hide 0 | |
$('#pause').show 0 | |
if (id) | |
if (sound) | |
sound.stop() | |
$('#progressbar').attr 'value', 0 | |
self = $('.track[data-id="' + id + '"]') | |
$('.track').removeClass 'playing' | |
self.addClass 'playing' | |
$('#cover img').attr 'src', self.data('cover') | |
SC.stream '/tracks/' + id, (track) -> | |
sound = track | |
sound.play() | |
else if (sound) | |
sound.play() | |
else | |
play $('#playlist .track').first().data 'id' | |
# Initialise the SoundCloud SDK | |
SC.initialize( | |
client_id: 'xxxxxx' | |
redirect_uri: '?' | |
) | |
# Get my selected playlist | |
SC.get '/playlists/xxxxxxxxx', (playlist) -> | |
# Display the playlist | |
$.each playlist.tracks, (index, track) -> | |
if (track.streamable) | |
$('#playlist').append [ | |
'<div class="track" data-id="' + track.id + '" data-format="' + track.original_format + '" data-cover="' + track.artwork_url + '">' | |
'<span class="index">' + (index + 1) + '</span>' | |
'<span class="title">' + track.user.username + ' - ' + track.title + '</span>' | |
'<span class="plays">' + track.playback_count.numberWithCommas() + '</span>' | |
'</div>' | |
].join('') | |
# Play button handler | |
$('#play').click -> | |
play() | |
# Pause button handler | |
$('#pause').click -> | |
pause() | |
# Previous button handler | |
$('#prev').click -> | |
play $('.playing').prev('.track').data('id') | |
# Next button handler | |
$('#next').click -> | |
play $('.playing').next('.track').data('id') | |
# Play the selected song | |
$('#playlist').on 'click', '.track', -> | |
play $(this).data 'id' | |
# Update the progress bar | |
setInterval (-> | |
if (sound && sound._player && sound._player._currentPosition && sound._player._duration) | |
$('#progressbar').attr 'value', (sound._player._currentPosition / sound._player._duration * 100) | |
if (sound._player._currentPosition == sound._player._duration) | |
play $('.playing').next('.track').data('id') | |
), 200 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment