简体   繁体   中英

Stream a song from SoundCloud using the Python API

I writing a little program that should stream a song from soundcloud.. my code is:

import soundcloud

cid="==="
cs="==="

un="===" 
pw="==="

client = soundcloud.Client(
    client_id=cid,
    client_secret=cs,
    username=un,
    password=pw
)
print "Your username is " + client.get('/me').username

# fetch track to stream
track = client.get('/tracks/293')

# get the tracks streaming URL
stream_url = client.get(track.stream_url, allow_redirects=False)

# print the tracks stream URL
print stream_url.location

It just printing the usernsame, and the track URL It prints something like this:

Your username is '==='
https://ec-media.soundcloud.com/cWHNerOLlkUq.128.mp3?f8f78g6njdj.....

Then, i want to play the MP3 from the URL. I can download it using urllib, but if it is a big file, it would take a lot of time.

What is the best way to stream the MP3? Thanks!!

Before using the solution I suggest here, you should be aware of the fact that you must credit SoundCloud somewhere in your application and possibly in your audio player that users will be seeing that it is served through SoundCloud. Doing the opposite will be unfair and probably violate their terms of usage.

track.stream_url is not the end point URL associated with the mp3 file. All the associated audio is only served 'on demand' when you send an http request with track.stream_url . Upon sending the http request you are being redirected to the actual mp3 stream (which is created just for you and will expire in next 15 mins).

So if you want to point the audio source you should at first get the redirect_url for the stream:

Below is the C# code which does what I am talking and it will give you the main idea - just convert it to Python code;

public void Run()
        {
            if (!string.IsNullOrEmpty(track.stream_url))
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(track.stream_url + ".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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM