【问题标题】:convert soundcloud page url to a direct url to the sound file将 soundcloud 页面 url 转换为声音文件的直接 url
【发布时间】:2014-04-02 06:26:52
【问题描述】:

我正在寻找一种从 soundcloud 获取 url 的方法,例如:

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

并将其转换为音频文件的直接链接,用作 html 音频标签的来源。

【问题讨论】:

  • 你在使用soundcloud api吗? developers.soundcloud.com/docs/api/sdks#
  • 这是否在他们的使用条款范围内?
  • CodeBlend:我假设是这样,因为他们有一个 API,允许您使用他们的声音文件创建自定义播放器。
  • @flauntster:还没有,但我认为我需要这个 api。

标签: javascript html audio soundcloud


【解决方案1】:

您从他们的 API 检索到的流 URL 不能直接在您自己的音频播放器中使用。

因为实际上,流 URL 不是您希望播放的音频的端点 URL。当您向流 url 发送 http 请求时,正在“按需”创建曲目的实际音频(mp3 格式)。 (没有实际的音频正在等待您随时流式传输 - 它们以二进制文件的形式存储在 AWS blob 存储中)

在请求带有唯一流 URL 的“音频”时,您将被重定向到带有音频的实际端点 - 这是物理路径,您可以直接在音频播放器中播放。此生成的音频 URL 的过期时间约为 15-20 分钟。

要实现我在这里所说的,你必须做一些类似的技巧: (代码在 C# 中,但你会明白的)

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

        }

AudioStreamEndPointUrl 是您的 mp3 音频将在接下来的 15-20 分钟内驻留的实际 URL。请注意,每次您想要流式传输歌曲时,都必须通过第一个流式传输 URL 重新请求歌曲。

因此,SoundCloud 的人实际上并不是不希望您在自定义音频播放器中流式传输曲目,而是与音频数据的存储意义有关。将此类数据保存为 blob 而不是实际的 mp3 文件可能对他们来说更便宜。

请注意,通过这样做,您应该公平对待,并在您的应用程序的某处真正归功于 SoundCloud。

【讨论】:

【解决方案2】:

Soundcloud 并不容易使用 HTML5 音频标签。这个问题在 SO 的其他地方被问到,我没有找到任何有效的答案。播放 soundcloud 链接的最佳方式是使用他们的 API。

您可以查看将 URL 更改为跟踪对象的解析调用。 http://developers.soundcloud.com/docs/api/reference#resolve

轨道对象具有stream_url 属性,您可以将其交给 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 2021-09-07
    • 2018-01-17
    • 1970-01-01
    • 2015-05-18
    • 2015-05-09
    • 1970-01-01
    相关资源
    最近更新 更多