Soundcloud API出错:无法读取null的属性'substr'

问题描述 投票:3回答:1

我正在尝试使用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()函数......

javascript soundcloud
1个回答
3
投票

你需要改变一些事情:

使用播放器小部件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&amp;auto_play=false&amp;hide_related=true&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;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版)

© www.soinside.com 2019 - 2024. All rights reserved.