0

I have looked for a while for the answer to this, and I cannot figure it out. I'm not sure if it's actually possible.

Basically, I have a URL to a sound cloud search (https://soundcloud.com/search?q=rick%20astley%20never%20gonna%20give%20you%20up), and I'm wondering if it's possible to instantly play the top result or download the top result. Thank you in advanced!

user6124043
  • 61
  • 1
  • 6

1 Answers1

0

So first its worth noting that Soundcloud has a Developers API, specifically a JavaScript SDK with Search functionality. They have documentation on how to do this.

However, I didn't want to go the registering App route for a lot of my projects. Although this might not be the recommended route, I'll add it as an answer anyways. If you look in the Network tab, you can see there are a bunch of requests and the one you really care about are the api-v2 related ones. You should find one that has something like this as a request URL:

enter image description here

https://api-v2.soundcloud.com/search?q=rick%20astley%20never%20gonna%20give%20you%20up&facet=model&client_id=OMITTED&limit=10&offset=0&linked_partitioning=1&app_version=OMITTED

This is the API request URL you Soundcloud makes behind the scenes specifically for its search API and you can see it passes in a bunch of GET parameters. I have OMITTED my specific credentials but as a user you have your own client_id and app_version so you'll need to simply put yours there.

If you want, you can manually go to this URL or just make a GET request to it via ajax and what gets returned back is a giant JSON like this:

enter image description here

And diving into it, you'll see there is a collection array property that has the results in it. Because you just want the first one, we can just look at the index in the array which comes out to be something like this:

{
"artwork_url": "https://i1.sndcdn.com/artworks-000074083588-q3fg6e-large.jpg",
"commentable": true,
"comment_count": 113,
"created_at": "2014-03-05T01:47:32Z",
"description": "",
"downloadable": true,
"download_count": 100,
"download_url": "https://api.soundcloud.com/tracks/137970300/download",
"duration": 266996,
"full_duration": 266996,
"embeddable_by": "all",
"genre": "Mashup",
"has_downloads_left": false,
"id": 137970300,
"kind": "track",
"label_name": "",
"last_modified": "2017-04-26T13:01:05Z",
"license": "all-rights-reserved",
"likes_count": 6307,
"permalink": "rick-astley-never-gonna-give",
"permalink_url": "https://soundcloud.com/andreasedstr-m/rick-astley-never-gonna-give",
"playback_count": 596829,
"public": true,

From here, you have two choices -- if you want, you can download it by looking at the property download_url or you can "play it". This part you might want to clarify what your definition of play is. You could just open the Soundcloud page and it should auto play I believe or you could embed it on your page and then somehow trigger a play. Whichever case, it's available.

To do this via JavaScript, you can probably just make a GET request to the URL above. I truncated the URL below. Remember you NEED to supply your client_id and app_version.

$.ajax({
  method: 'GET',
  url: 'https://api-v2.soundcloud.com/search?q=rick%20astley%20never%20gonna%20give%20you%20up',
  dataType: 'json',
  contentType: "application/json; charset=utf-8",
  success: function (data) {
    // do stuff here
    // as mentioned because you care about the first index in the property above 
    // you would most likely interact with it by accessing data.collection[0]
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

One last thing if you run to CORS its because the service doesn't see you as an actual human. See here

Hope this helps.

Community
  • 1
  • 1
aug
  • 11,138
  • 9
  • 72
  • 93