使用Python API从SoundCloud流式播放歌曲

使用Python API从SoundCloud流式播放歌曲,python,python-2.7,mp3,soundcloud,playback,Python,Python 2.7,Mp3,Soundcloud,Playback,我正在写一个小程序,可以从soundcloud流式播放一首歌。。 我的代码是: 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').u

我正在写一个小程序,可以从soundcloud流式播放一首歌。。 我的代码是:

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
它只是打印usernsame和跟踪URL 它打印出如下内容:

Your username is '==='
https://ec-media.soundcloud.com/cWHNerOLlkUq.128.mp3?f8f78g6njdj.....
然后,我想从URL播放MP3。我可以使用urllib下载它,但如果它是一个大文件,则需要很多时间

播放MP3的最佳方式是什么?
谢谢

在使用我在此建议的解决方案之前,您应该意识到这样一个事实,即您必须在应用程序中的某个地方以及可能在音频播放器中使用SoundCloud,用户将看到它是通过SoundCloud提供的。相反的做法将是不公平的,并且可能违反他们的使用条款

track.stream\u url
不是与mp3文件关联的端点url。 当您使用
track.stream\u url
发送http请求时,所有相关音频仅“按需”提供。发送http请求后,您将被重定向到实际的mp3流(仅为您创建,将在未来15分钟内过期)

因此,如果要指向音频源,首先应获取流的重定向url:

下面是C#代码,它实现了我所说的,它将为您提供主要思想-只需将其转换为Python代码

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();

        }