Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  28] [ 4]  / answers: 1 / hits: 10183  / 10 Years ago, wed, april 2, 2014, 12:00:00

I am looking for a way to take a url from soundcloud such as this:



https://soundcloud.com/cameron-mitchell-28/ancient-greek



and convert it to a direct link to the audio file, to be used as the source for an html audio tag.


More From » html

 Answers
2

The stream Url you retrieved from their API can't be directly used within your own audio player.


Because actually, the stream URL is not the end-point URL of the audio you are expecting to play. The actual audio (in mp3 format) of the tracks are being created 'on demand' when you send an http request to the stream url. (There is no such thing as the actual audio is waiting ready for you to be streamed anytime - they are stored as binaries in AWS blob storages)


Upon requesting the 'audio' with the unique stream URL, you are being redirected to the actual end-point with the audio- this is the physical path and you can play it directly within your audio player. This generated audio URL has an expiration time like 15-20 minutes.


To achieve what I am talking about here, you will have to do some tricks similar to this:
(Code is in C# but you will get the idea)


public void Run()
{
if (!string.IsNullOrEmpty(TrackUrl))
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(TrackUrl + ".json?client_id=YOUR_CLIENT_ID");
request.Method = "HEAD";
request.AllowReadStreamBuffering = true;
request.AllowAutoRedirect = true;
request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
}
}

private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);


using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
{
this.AudioStreamEndPointUrl = myResponse.ResponseUri.AbsoluteUri;
this.SearchCompleted(this);
}
myResponse.Close();

}

The AudioStreamEndPointUrl is the actual URL where your mp3 audio will reside for the next 15-20 mins. Note that each time you would like to stream a song, you will have to re-request the song via the first stream URL..


So it's not actually SoundCloud guys don't want you to stream the tracks in your custom audio player but it's rather related with the storage sense of the audio data. Keeping such data as blob rather than the actual mp3 file is perhaps cheaper for them.


Note that by doing this you should play fair and actually credit the SoundCloud somewhere in your application.


[#46360] Tuesday, April 1, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarkulisesa

Total Points: 422
Total Questions: 93
Total Answers: 112

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
clarkulisesa questions
Mon, Feb 24, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;