Mattia
Mattia

Reputation: 51

Video.js - loadedmetadata event never fires

I am having trouble with videojs: when attaching an eventlistener to the "loadedmetadata" event, the callback function is never executed.

The best explanation I found seems to be that some events may fire before Video.js binds the event listeners: Video.js - loadeddata event never fires.

Unfortunately, the solution proposed in this post does not seem to work for me.

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<link href="http://vjs.zencdn.net/4.12/video-js.css" rel="stylesheet">
<script src="http://vjs.zencdn.net/4.12/video.js"></script>
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script type="text/javascript">
        function init() {
            var video = document.getElementById('myVideo');
            video.addEventListener("loadedmetadata", function () {
                alert("test");
            });
        }
        window.addEventListener("load", init);
    </script>
</head>
<body>
    <video id='myVideo' class="video-js vjs-default-skin" controls data-setup='{}'>
        <source id='mp4' src="http://goo.gl/fAHXgj" type='video/mp4'>
        <source id='webm' src="http://goo.gl/03LOHW" type='video/webm'>
    </video>
</body>
</html>

When I launch it from Visual Studio (2013), the code above produces an alert pop up only in IE(11); it does not work with Firefox nor Chrome.

When I publish it on my website it never works. What am I missing? Thank you for reading!

Upvotes: 4

Views: 16497

Answers (2)

Aaria Carter-Weir
Aaria Carter-Weir

Reputation: 1679

As misterben said, use the .on method. But put it in the ready callback or it will not work:

var player = videojs('myVideo');
player.ready(function(){
    this.on('loadedmetadata', function(){ alert('lmd'); })
});

Source: just figured this out myself. I know misterben has mentioned using the ready callback but you may not have known that you need to use it.

Upvotes: 6

misterben
misterben

Reputation: 7821

As you're using video.js you need to use it's API, e.g.

videojs('myVideo').on('event',function);

Even better, remove the data-setup attribute and create the player by calling the if to videojs() along with a callback function to be executed as soon as the player is ready:

videojs('myVideo', {}, function(){
  this.on('loadedmetadata', function(){
    alert('lmd');
  });
});

Upvotes: 2

Related Questions