feross/yt-player

How to know when metadata has loaded?

san-kumar opened this issue · 3 comments

It says that player.getDuration will return 0 unless metadata has loaded.

But I can't find any event for that.

Is there a player.on('ready' type event that I can use to make sure it's loaded before I call getDuration? I tried it but it doesn't seem to work.

Any ideas?

I just released a new version 2.7.0 with a 'ready' event that you can use. When you load further videos, you can use the 'cued' event.

Thanks for the update. Unfortunately cued does not fire unless I call player.play first. Try this code out

const YTPlayer = require('yt-player')
const player = new YTPlayer('#player')

player.on('cued', () => {
  console.log(player.getDuration()) // => returns 0
});

player.load('GKSRyLdjsPA')

Metadata is not available during the 'cued' event. You need to wait until the 'playing' event.

Try updating the latest version and then try this:

const YTPlayer = require('yt-player')
const player = window.player = new YTPlayer('#player')

player.on('playing', () => {
  console.log('PLAYING', player.getDuration()) // => returns 351.521
})

player.load('GKSRyLdjsPA', false)
player.play()