Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.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 使用HTML5音频和jQuery调用对象(this)的引用_Javascript_Jquery_Html_Html5 Audio - Fatal编程技术网

Javascript 使用HTML5音频和jQuery调用对象(this)的引用

Javascript 使用HTML5音频和jQuery调用对象(this)的引用,javascript,jquery,html,html5-audio,Javascript,Jquery,Html,Html5 Audio,我正在创建自己的HTML5音频播放器,能够处理播放列表。我创建了一个自定义的myPlaylist对象,其中包含所有play()、pause()、stop()以及其他所需的功能。这一切都正常工作,但此外,我需要知道音频文件何时结束,以便自动开始播放下一个音频文件 以下是我正在使用的代码的相关部分: function myPlaylist(){ var player = document.createElement('audio'); var audio = $(player).g

我正在创建自己的HTML5音频播放器,能够处理播放列表。我创建了一个自定义的
myPlaylist
对象,其中包含所有
play()
pause()
stop()
以及其他所需的功能。这一切都正常工作,但此外,我需要知道音频文件何时结束,以便自动开始播放下一个音频文件

以下是我正在使用的代码的相关部分:

function myPlaylist(){

    var player = document.createElement('audio');
    var audio = $(player).get(0);

    this.next = function next(){
        // Picks next song in the playlist and plays it
        ...
    };

    $(audio).bind('ended', function() {
        alert("Song is finished!");
        // Here I want to call to my next() function
    });
}
我还不知道怎么做。我已经尝试过几种组合,比如
$(this).next()
,它看起来最合理,实际上显示了警报,但什么也不做?;还有
this.next()
,它也显示了警报,但随后显示了一个错误,因为
this
引用的是HTML5音频元素,它没有
next()
功能

我还尝试了另一种方法,使用

audio.onended = function(){
    alert("Song is finished!");
    $(this).next();
}; 
但这些甚至不会触发警报。另外,
audio.end
不起作用

所以,我现在基本上一无所知,有人知道我做错了什么吗?提前谢谢

哦,我已经在MacOSX的最新版本的谷歌浏览器和Safari中测试了所有这些


编辑按照中给出的建议,我还尝试了以下代码

player.addEventListener("ended", function() {
    alert("Song is finished!");
    $(this).next();
});

尽管第一个警报正确显示,但它们都不起作用


编辑2使用我遇到的搜索,这可能也与我的问题有关,因此为了避免引用
时可能出现的任何问题,我添加了一个引用对象本身的新变量,因此现在我基本上在处理:

function myPlaylist(){

    var player = document.createElement('audio');
    var audio = $(player).get(0);
    var me = $(this);

    this.next = function next(){
        // Picks next song in the playlist and plays it
        ...
    };

    $(audio).bind('ended', function() {
        alert("Song is finished!");
        me.next();
    });
}
但是我得到一个错误,说对象没有方法
next()


我不知道我还能尝试什么。。。如有任何额外信息,我们将不胜感激,谢谢

有一个HTML5播放列表示例处理
end
事件,如果有帮助的话

在事件处理程序中,您引用了
this
,但在此上下文中
this
引用了捕获事件的DOM元素,即
audio
元素。。请尝试以下方法:

function myPlaylist(){

    var self = this;
    var player = document.createElement('audio');

    this.next = function (){
        // Picks next song in the playlist and plays it
        ...
    };

    player.addEventListener("ended", function() {
        alert("Song is finished!");
        self.next();
    });

}

Chrome和Safari中的事件名称通常不会以on开头(如audio.oneded),这在我看来更像IE。您在第二个示例中是否尝试过audio.End?是的,我也尝试过,但结果相同,未显示警报。但我不知道事件名称取决于浏览器。谢谢你的提示。谢谢你,我还没看到那个。我尝试过使用上面提到的选项,但似乎也不起作用。我会用新的信息编辑这个问题。我认为两者都是正确的,但无论如何,它也不起作用:SOh!用它工作!!我只是尝试同样的方法,但使用jQuery,即var me=$(this);它不起作用,但它与您的代码一起工作!非常感谢你!好的,酷。。在javascript中使用关键字
this
时需要小心。。查看
上的Mozilla文档:谢谢链接,会很方便的。顺便说一句,正如我前面提到的,当函数有名称时,它会工作,也就是说,我一直使用'this.next=function next(){…}',所以这不会真正影响结果。
function myPlaylist(){

    var self = this;
    var player = document.createElement('audio');

    this.next = function (){
        // Picks next song in the playlist and plays it
        ...
    };

    player.addEventListener("ended", function() {
        alert("Song is finished!");
        self.next();
    });

}