使用WebAudio播放Soundcloud音轨

时间:2015-05-05 22:22:07

标签: soundcloud web-audio

我正在探索WebAudio和SoundCloud的可视化程序,但似乎无法通过基本的测试阶段。这是我用于测试的代码,但我听不到任何声音。我错过了什么吗?

<script src="http://connect.soundcloud.com/sdk.js"></script>
<script>
var context = new AudioContext(),
audio = new Audio(),
source,     
url = 'http://api.soundcloud.com/tracks/204082098/' + '?client_id=(used my client id)';

audio.src = url;
source = context.createMediaElementSource(audio);
source.connect(context.destination);
source.mediaElement.play();

</script>

2 个答案:

答案 0 :(得分:0)

您正试图通过域边界访问声音。由于SoundCloud不支持使用CORS进行跨域访问,因此当连接Web Audio时(因为Web Audio允许您检查位),这将不再有效。

答案 1 :(得分:0)

我想问题是如何加载mp3文件而你最后忘记了/ stream。

此代码有效:

audioContext = new AudioContext();
var source = audioContext.createBufferSource();
var gainNode = audioContext.createGain();
source.connect(gainNode);
gainNode.connect(audioContext.destination);
gainNode.gain.value = 0.5;
source.connect(audioContext.destination);

  function SCkickOffSampleDownload( sample ) {
    var url = new URL(sample+ '?client_id=679877a8ddb9badc6a2a75373c5f3de7');
    var request = new XMLHttpRequest();
    request.open("GET", url, true);
    request.responseType = "arraybuffer";
    request.onload = function() {
      audioContext.decodeAudioData( request.response,
        function(buffer) {
          console.log("sample loaded!");
          sample.loaded=true;
          source.buffer = buffer;
          source.start(0);
      },
      function() { console.log("Decoding error! ");} );
    }
    sample.loaded = false;
    request.send();
  }

var sample = 'http://api.soundcloud.com/tracks/204082098/stream';

SCkickOffSampleDownload( sample );

http://jsfiddle.net/iambnz/s7999f8t/

一些相关文档:

我基于SoundCloud数据构建了一个采样器,cwilso可能知道UI。 ;-) 您可以在此处查看:http://www.dope-dj-culture.com