最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - mediaelement 'playing' event tracking - Stack Overflow

programmeradmin3浏览0评论

I have the following simple code that initiates the mediaelement audio plugin:

$('audio,video').mediaelementplayer({
    audioWidth: 300,
    audioHeight: 30
});

I need to track the playing event; I've tried the following code without any luck:

var player = document.getElementsById('player2');
player.playing = function(e) {
   alert('OMG! You played a song!');
}

I have the following simple code that initiates the mediaelement audio plugin:

$('audio,video').mediaelementplayer({
    audioWidth: 300,
    audioHeight: 30
});

I need to track the playing event; I've tried the following code without any luck:

var player = document.getElementsById('player2');
player.playing = function(e) {
   alert('OMG! You played a song!');
}
Share Improve this question edited Dec 22, 2020 at 14:42 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 6, 2012 at 23:36 InvalidSyntaxInvalidSyntax 9,50521 gold badges87 silver badges137 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

Try this:

# using jquery (on, bind, etc)...
$('#player1').bind('playing', function(e) { 
   console.log('bind - playing') 
});

# addEventListener 
var player = document.getElementById('player1'); 
player.addEventListener('playing', function(e) {
   console.log('addEventListener - playing') ;
});

Ok so I've figured it out...

$('audio,video').mediaelementplayer({
    audioWidth: 300,
    audioHeight: 30,

    success: function(mediaElement, domObject) {
        mediaElement.addEventListener('loadeddata', function() {
            console.log('addEventListener - loadeddata')            
        }, false);
    },
    error: function() {
        //alert('Error setting media!');
    }

});

A little late but had to solve this one myself. I noted that the MediaElement docs say that the success function first argument is a mediaElement object. It just so happens that the player html is wrapped inside a element, no matter what type of video it is. You can't access it directly as a jQuery object but a simple tweak fixes it all.

The following is used to fetch the closest player no matter which element you pass in

function getVideo(el) {
    var $el = $(el);

    if ($el.find('mediaelementwrapper').length) {
        return $el.find('mediaelementwrapper')[0];
    } else if ($el.parents('mediaelementwrapper').length) {
        return $el.parents('mediaelementwrapper')[0];
    }

    return $el.is('mediaelementwrapper') ? $el : false;
}

Notice we have to access it via [0] returning the document node rather than jQuery object.

But from there we can use it like this.

$video = getVideo('#playerID');

$video.addEventListener('playing', function () {
    debugger;
}, false);

Or do it without the helper

$video = jQuery('#playerID mediaelementwrapper')[0];
发布评论

评论列表(0)

  1. 暂无评论