Home > OS >  getting NaN when asking for video duration
getting NaN when asking for video duration

Time:10-11

Can anyone help me understand why console.log(this.getPlayerDuration()) inside of setAttendancePromptTimeLimit() is returning NaN?

// video-player.js    

const videoP = document.querySelector('#in-course-player')

class videoPlyer {
  constructor(videoElem){
        this.vidPlayer = videoElem
    }
  play =  function(cb){
    arguments.length ? cb : console.log('play')
  }
    
  addListener = (elem, event, cb) => {
        elem.addEventListener(event, cb)
    }

  getPlayerCurrentTime = () => this.vidPlayer.currentTime

  getPlayerDuration = () => this.vidPlayer.duration

  showVideoDuration = function(cb){
    arguments.length ? cb : this.addListener(this.vidPlayer, 'loadedmetadata', () => 
        console.log(this.getPlayerDuration()))
    }


  setAttendancePromptTimeLimit = cb => {

    // returns NAN
    console.log(this.getPlayerDuration())
  }

  init = () => {
    //returns 16.1
    this.showVideoDuration()  
    //returns NAN 
    this.setAttendancePromptTimeLimit()    
  }
 }

 const coursePlayer = new videoPlyer(videoP)
 coursePlayer.init()

CodePudding user response:

[...] If no media data is available, the value NaN is returned. [...]
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/duration

  • Related