我正在尝试使用Soundcloud API(本地)编写Widget。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Soundcloud musics</title>
</head>
<body>
<h1>Soundcloud musics</h1>
<iframe id="iframe"
class="iframe"
width="100%"
height="465"
scrolling="no"
frameborder="no">
</iframe>
<script src="api.js"></script>
<script>
var player;
player = SC.Widget(document.getElementById('iframe'));
console.debug(player);
player.load('https://soundcloud.com/somesong', null);
</script>
</body>
</html>
我从运行它得到的错误是:
Uncaught TypeError: Cannot read property 'substr' of null
E @ api.js:204
d @ api.js:328
n.exports.v @ api.js:326
(anonymous function) @ index.html:21
我已经将soundcloud的最小化api代码输入到js美化器中,因此我可以跟踪错误。这是代码:https://jsfiddle.net/0anL7jfs/
难道我做错了什么 ?似乎问题来自SC.Widget()函数......
你需要改变一些事情:
使用播放器小部件api url和属性在iframe中定义
src
:
<iframe id="iframe"
class="iframe"
width="100%"
height="465"
scrolling="no"
frameborder="no"
src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/76067623&auto_play=false&hide_related=true&show_comments=true&show_user=true&show_reposts=false&visual=true">
</iframe>
使用
widget.bind
绑定您的活动:
<script type="text/javascript">
(function(){
var iframeElement = document.getElementById('iframe');
var widget = SC.Widget(iframeElement);
widget.bind(SC.Widget.Events.READY, function () {
console.log('Ready');
widget.bind(SC.Widget.Events.PLAY, function () {
widget.getCurrentSound(function (sound) {
console.log(sound.title);
});
});
widget.bind(SC.Widget.Events.FINISH, function () {
console.log('Finished');
});
});
}());
</script>
例:
http://jsfiddle.net/dqz1jmzo/(JQuery版)