Simple SoundCloud playlist creator using Soundcloud API.
- I develop these simple SoundCloud player to sharpen my JavaScript knowledge.
- In
main.js
file there are 4 main sections to resolve as you can see. - Those 4 sections should be developed to get the final product “Soundcloud Player”.
- In the future there could be subsections as I develop.
- As the time of writing this blog post SoundCloud has stopped providing free API keys, so I’m gonna use a public API key that I found on the Internet. might get broken, let’s see
- This post is going to get updated as I develop this.
Query Soundcloud API.
- Even though this is the 2nd section, I want to tackle this first.
- You can read more about the “Search” function in SoundCloud docs.
- I copied the code in SoundCloud docs and refactored as I want (Removed
<script>
tags, removed license filter and put the “Client Id”)
SC.initialize({
client_id: 'Client Id'
});
SC.get('/tracks', {
q: 'buskers',
}).then(function(tracks) {
console.log(tracks);
});
- Then I created
SoundCloudAPI
object andSoundCloudAPI.init()
,SoundCloudAPI.getTracks()
functions, so I can call those functions easily.
SoundCloudAPI.init = function() {
SC.initialize({
client_id: 'Client Id'
});
}
SoundCloudAPI.init();
SoundCloudAPI.getTracks = function (inputValue) {
SC.get("/tracks", {
q: inputValue
}).then(function (tracks) {
if(inputValue === "") {
alert("You have entered an empty keyword");
} else {
console.log(inputValue);
SoundCloudAPI.renderTracks(tracks);
}
});
};
- Used an
if
condition to avoid searching blank keywords.
Displaying Cards.
- Created
renderTracks()
function to print/render all the result tracks as cards, so the cards can be rendered dynamically and easily looped through. - Inside the function I created the card dynamically using
createElement()
,classList
andappendChild()
methods. - Finally called the API endpoints dynamically.
Add to playlist and play.
- Grabbed the code for embedded player from SoundCloud docs.
SC.oEmbed('https://soundcloud.com/forss/flickermood', {
auto_play: true
}).then(function(embed){
console.log('oEmbed response: ', embed);
});
- Modified the code to place the “embedded player” on the left sidebar and pass the music dynamically (Music passing
clickEvent
is coded inside “Displaying Cards” section) - Used
insertBefore()
method to stack the music in the.js-playlist
div. - Then I used
localStorage
to save the created playlist for the user even if exited.
Search Function
- Created
Search
object. - Created 2 functions
pressEnter()
andclick()
using the object. - Those two functions are used to get the search keyword when pressed “enter” or clicked the search icon.
Reset Function
- As a extra, added a “Reset” button to clear the
localStorage()
and reload the page.