如何在soundcloud javascript api中暂停流媒体
我想允许播放声音文件并暂停。文档显示了如何播放流文件:如何在soundcloud javascript api中暂停流媒体,javascript,jquery,api,soundcloud,soundmanager2,Javascript,Jquery,Api,Soundcloud,Soundmanager2,我想允许播放声音文件并暂停。文档显示了如何播放流文件: $("#stream").live("click", function(){ SC.stream("/tracks/{'track-id'}", function(sound){ sound.play(); }; 它使用声音管理器来流媒体,并使用它的一些对象和方法。。。例如:暂停() 下面是我认为应该有效的代码: var is_playing="
$("#stream").live("click", function(){
SC.stream("/tracks/{'track-id'}", function(sound){
sound.play();
};
它使用声音管理器来流媒体,并使用它的一些对象和方法。。。例如:暂停()
下面是我认为应该有效的代码:
var is_playing="false";
$("#stream").live("click", function(){
SC.stream("/tracks/{'track-id'}", function(sound){
if (is_playing==="true")
{
sound.pause();
is_playing = "false";
console.log(is_playing);
}
else if (is_playing === "false")
{
sound.play();
is_playing = "true";
console.log(is_playing);
}
});
但是,它会继续播放曲目而不暂停。有什么想法吗?
谢谢。由于关于这个问题的答案很少,我想我会回答我自己的问题 我继续下载了Sound Manager 2,并将必要的文件放在需要的地方(您可以在Sound Manager 2页面的基本教程部分找到) 诀窍是找到与我想要嵌入的用户关联的SoundCloud mp3。为此,我必须使用RESTClient并输入适当的api url。 看起来像这样 {MyClient_ID_number} 是指向轨迹id=“12758186”的常规指针。接下来是JSONP包含我的个人客户端id,以验证我对soundcloud服务器的请求 一旦我有了这个url,我在我的浏览器中尝试了它,它将我重定向到另一个带有HTML5 mp3播放器的**url**,该播放器会自动播放这首歌 我从soundcloud文档中得到了这样做的想法: 然后我复制了这个url,并将原始问题中的代码混合到上面提到的基础教程() 最终代码:
var is_playing = false;
$("#stream").live("click", function(){
soundManager.setup({
url: './swf/soundmanager2.swf',
onready: function() {
var mySound = soundManager.createSound({
id: 'aSound',
url: {**another url**},
autoplay: false
});
if (is_playing===false)
{
mySound.play();
is_playing = true;
}
else if (is_playing=== true)
{
mySound.stop();
is_playing = false;
}
},
ontimeout: function() {
// Hrmm, SM2 could not start. Missing SWF? Flash blocked? Show an error, etc.?
}
});
});
var is_playing=false;
soundManager.setup({
url:“/swf/soundmanager2.swf”,
A:错,
onready:function(){
var mySound=soundManager.createSound({
id:“aSound”,
url:'sonidos/sonido1.mp3',
自动播放:错误
});
$(“#流”).live(“单击”,函数(){
如果(正在播放==错误){
mySound.play();
你在玩吗;
}else if(正在播放==true){
mySound.stop();
正在播放=错误;
}
});
},
/*ontimeout:function(){
//Hrmm,SM2无法启动。缺少SWF?闪存被阻止?显示错误等。?
}*/
});
只需将要触发暂停函数的事件链接到传递到SC.stream()回调函数的“sound”对象
下面代码中的表单接受类似这样的声音云链接:(因此,一旦应用程序处于活动状态,请将其粘贴到表单中,然后单击“加载”)。此外,注册应用程序时,您必须提供soundcloud为您提供的自己的客户端id,这只需一分钟左右
<!DOCTYPE html>
<html>
<head></head>
<body>
<script src="http://connect.soundcloud.com/sdk.js"></script>
<script>
function load_sound() {
var track_url = document.getElementById("sc_url").value;
SC.initialize({
client_id: 'client_id'
});
SC.get('/resolve', {url: track_url}, function(track) {
console.log("the track id is: " + track.id);
SC.stream("/tracks/" + track.id, function(sound) {
var is_playing = true;
sound.play();
document.getElementById("stream").onclick = function(){
if(is_playing === false){
sound.resume();
is_playing = true;
} else if(is_playing === true){
sound.pause();
is_playing = false;
}
};
});
});
}
</script>
<form>
<input id="sc_url" type="url">
<input type="button" onclick="load_sound();" value="load">
</form>
<button id="stream">pause/resume</button>
</body>
</html>
函数加载_sound(){
var track_url=document.getElementById(“sc_url”).value;
SC.initialize({
客户id:“客户id”
});
SC.get('/resolve',{url:track_url},函数(track){
console.log(“轨迹id为:“+track.id”);
SC.stream(“/tracks/”+track.id,函数(声音){
var是_playing=true;
声音。播放();
document.getElementById(“流”).onclick=function(){
如果(正在播放===错误){
健全。恢复();
你在玩吗;
}else if(正在播放===true){
声音。暂停();
正在播放=错误;
}
};
});
});
}
暂停/恢复
您必须使用sound.stop()代码>
还有一个内置功能可切换声音的播放/暂停。
使用sound.togglePause()代码>来做这件事
SC.stream("/tracks/13680213", function(sound) {
SC.sound = sound;
});
这意味着您没有在SC.stream构造函数中编写事件侦听器,那么您可以随意访问以下内容(以及更多内容):
SC.sound.play();
SC.sound.pause();
SC.sound.stop();
SC.sound.pause();
SC.sound.playState;
var正在播放=“false”
。。你应该用布尔值。但是,这并没有解决问题。请正确缩进,否则很难阅读源代码,请参阅
SC.stream("/tracks/13680213", function(sound) {
SC.sound = sound;
});