javascript - 在 HTML 5 mp3 播放器 (audio.js) 中播放 SoundCloud 流 uri

标签 javascript html5-audio soundcloud audio-streaming audiojs

我想从 SoundCloud 加载轨道并在 HTML5 音频播放器中播放:http://kolber.github.io/audiojs/

我让它处理 .mp3 文件,就像他们在演示中所做的那样。我也已成功连接到 SoundCloud api 并将 src 放在正确的位置。但是,流 uri:api.soundcloud .com/tracks/75868018/stream?client_id=ed34fc3159859e080af9eb55f8c3bb16(这是一个假的客户端 ID,我无法发布链接)不起作用。

我都试过 sound.stream_url 和 sound.uri 详见此处:developers.soundcloud .com(无法发布链接)

如何在 mp3 播放器中播放来自 Soundcloud api 的流链接?

下面是我的代码

HTML - 剥离

<!DOCTYPE html>
<html lang="en">
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script> // load jquery
<script src="http://connect.soundcloud.com/sdk.js"></script> // connect to soundlcoud
<script src="jb3.js" type="text/javascript"></script> // run script to load track into <li> element in DOM
<script src="audio.min.js"></script> // load audio,js script for audio player
</head>
<body>


<audio ></audio>
<h2>Playlist</h2>
  <ol id="userPlaylist">
    <li><a href="#" data-src="http://kolber.github.io/audiojs/demos/mp3/02-juicy-r.mp3">dead wrong intro</a></li> //woorking track using .mp3

<li class="playing">
<a data-src="http://api.soundcloud.com/tracks/75868018/stream?client_id=ed34fc3159859e080af9eb55f8c3bb16" href="#">sc SONG</a>
</li>
//STREAM THAT IS NOT WORKING

</body>
</html>

我的 JAVASCRIPT - jb3.js

function addSc() {

SC.get("/tracks/75868018", {}, function(sound){
alert("Sound URI: "+sound.uri);
 $("ol#userPlaylist").append('<li> <a href="#" data-src="'+sound.stream_url+'.json/stream?client_id=ed34fc3159859e080af9eb8558c3bb16">sc     SONG</li>');
});
}

window.onload = function() {
SC.initialize({
client_id: 'ed34fc3159859e080af9eb55f8c3bb16'
});

addSc();
};

最佳答案

我已经使用默认播放器和带有 sc-urls 的 demos/test6.html 对其进行了测试,并且工作正常。

你在哪里创建播放器? 看起来你的 url 似乎是错误的(请检查下面的 Ex 2)

示例 1

HTML

<audio src="http://api.soundcloud.com/tracks/148976759/stream?client_id=201b55a1a16e7c0a122d112590b32e4a" preload="auto"></audio>

JS

audiojs.events.ready(function() {
        audiojs.createAll();
      });

http://jsfiddle.net/iambnz/6dKLy/

EXAMPLE 2 with playlist (test6 just with sc urls)

HTML

<div id="wrapper">
<h1>Test .. :-) <em>(2014)</em></h1>
<audio preload></audio>
<ol>
<li><a href="#" data-src="http://api.soundcloud.com/tracks/148976759/stream?client_id=201b55a1a16e7c0a122d112590b32e4a">Phil Collins Edit</a></li>
<li><a href="#" data-src="http://api.soundcloud.com/tracks/75868018/stream?client_id=ed34fc3159859e080af9eb55f8c3bb16">Your track</a></li>
</ol>
</div>

JS

 $(function() { 
        // Setup the player to autoplay the next track
        var a = audiojs.createAll({
          trackEnded: function() {
            var next = $('ol li.playing').next();
            if (!next.length) next = $('ol li').first();
            next.addClass('playing').siblings().removeClass('playing');
            audio.load($('a', next).attr('data-src'));
            audio.play();
          }
        });

        // Load in the first track
        var audio = a[0];
            first = $('ol a').attr('data-src');
        $('ol li').first().addClass('playing');
        audio.load(first);

        // Load in a track on click
        $('ol li').click(function(e) {
          e.preventDefault();
          $(this).addClass('playing').siblings().removeClass('playing');
          audio.load($('a', this).attr('data-src'));
          audio.play();
        });
        // Keyboard shortcuts
        $(document).keydown(function(e) {
          var unicode = e.charCode ? e.charCode : e.keyCode;
             // right arrow
          if (unicode == 39) {
            var next = $('li.playing').next();
            if (!next.length) next = $('ol li').first();
            next.click();
            // back arrow
          } else if (unicode == 37) {
            var prev = $('li.playing').prev();
            if (!prev.length) prev = $('ol li').last();
            prev.click();
            // spacebar
          } else if (unicode == 32) {
            audio.playPause();
          }
        })
      });

http://jsfiddle.net/iambnz/VL7n8/

检查:http://kolber.github.io/audiojs/demos/test6.html

关于javascript - 在 HTML 5 mp3 播放器 (audio.js) 中播放 SoundCloud 流 uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24232025/

相关文章:

javascript - 使用 R Shiny 更改点击点的颜色并保持不变

javascript - 如何在 Safari 中获取音频文件的持续时间?

ios - 不播放来自 stream_url ing SoundCloud API 的声音

javascript - HTML5 音频未在本地主机的 React 应用程序中播放

javascript - 单击时切换自定义 SoundCloud 播放暂停图像

javascript - Waveform.js - 波形中较粗线的算法

javascript - render() 内外的函数

javascript - 如何在跟踪浏览器窗口高度的网页上创建滚动区域?

javascript - 使用 options.items 与标题的 JQuery UI 工具提示扩展

javascript - Web Audio API 如何修改混响尾音、房间大小和其他环境变量