Play SoundCloud stream uri in HTML 5 mp3 player (audio.js)

I would like to load tracks from SoundCloud and have them played in a HTML5 audio player provided by: http://kolber.github.io/audiojs/

I have it working with .mp3 files, as they do in the demo. I have also successfully connected to the SoundCloud api and placed the src in the right place. However the stream uri : api.soundcloud .com/tracks/75868018/stream?client_id=ed34fc3159859e080af9eb55f8c3bb16 (it's a fake client id, I can't post link) does not work.

I have tried using both sound.stream_url & sound.uri detailed here: developers.soundcloud .com (cannot post link)

How do I play a stream link from the Soundcloud api in an mp3 player?

Below is my code

HTML - Stripped

<!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>

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

Answers:

Answer

I have tested it with the default player and demos/test6.html with sc-urls, and it works fine.

Where do you create the player? Looks like your url seems to be wrong (pls check Ex 2 below)

EXAMPLE 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/

Check: http://kolber.github.io/audiojs/demos/test6.html

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.