Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用SoundCloud API暂停声音_Javascript_Audio_Streaming_Soundcloud - Fatal编程技术网

Javascript 使用SoundCloud API暂停声音

Javascript 使用SoundCloud API暂停声音,javascript,audio,streaming,soundcloud,Javascript,Audio,Streaming,Soundcloud,我是JS新手,我很难让当前正在播放的音频暂停或停止使用SoundCloud JavaScript SDK。我使用的是平均堆栈 在“就绪”功能中,我初始化SC播放器: SC.initialize({ client_id: "my_client_ID", redirect_uri: "http://example.com/callback.html", }); ... $('#trackList table tbody').on('click', 'td a.linkplaytr

我是JS新手,我很难让当前正在播放的音频暂停或停止使用SoundCloud JavaScript SDK。我使用的是平均堆栈

在“就绪”功能中,我初始化SC播放器:

SC.initialize({
    client_id: "my_client_ID",
    redirect_uri: "http://example.com/callback.html",
});

...

$('#trackList table tbody').on('click', 'td a.linkplaytrack', playTrack);
$('#trackList table tbody').on('click', 'td a.linkpausetrack', pauseTrack);
playTrack功能工作正常,音频开始播放:

function playTrack(event){
    event.preventDefault();

    SC.stream("/tracks/293", function(sound){
        sound.play();
    });
};
但由于某些原因,pauseTrack函数不起作用:

function pauseTrack(event){
    event.preventDefault();

    SC.stream("/tracks/293", function(sound){
        sound.pause();
    });
};

任何帮助、见解或建议都将不胜感激

每次调用SC.stream时,您都在创建一个新的声音对象。暂停是工作,只是在一个不同的声音比一个播放。您可以存储流以供以后使用:

var stream;

function playTrack(event) {
  e.preventDefault();
  if(!stream) {
    SC.stream("/tracks/293", function(sound){
        stream = sound;
        stream.play();
    });
  }
  else {
   stream.play();
  }
}


function pauseTrack(event) {
  if(stream) {
    stream.pause();
  }
}