Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

Soundcloud does not making using the HTML5 audio tag to easy. This question is asked in other places on SO and there aren't any working answers that I've found. The best way to play soundcloud links is using their API.

You can take a look at the resolve call to change the URLs into track objects. http://developers.soundcloud.com/docs/api/reference#resolve

The track objects have the stream_url property that you can hand to the play function in the soundcloud API. http://developers.soundcloud.com/docs/api/reference#tracks

    <script src="http://connect.soundcloud.com/sdk.js"></script>
    <script>
    SC.get('/resolve?url=' + 'https://soundcloud.com/cameron-mitchell-28/ancient-greek', function(track){
        SC.stream(track.stream_url, function(sound){
            sound.play();
        });
    });
    </script>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top