Search code examples
javascriptgoogle-chromevideo.js

VideoJs TextTracks Cues empty


I need to be able to get the Cues array from TextTracks object in VideoJs.

I have the following code:

videojs("example_video_1", {}, function(){
   // Player (this) is initialized and ready.
    var aTextTrack =  this.textTracks()[0];

    aTextTrack.show();
    var theArray = this.textTracks()[0]['$'];
    console.log(theArray);
    console.dir(this.textTracks()[0]['$']);
    // EXAMPLE: Start playing the video.
    this.play();

}); 

enter image description here

The var theArray prints as empty but console.dir prints the contents of the Array. How can I get these values? I know its related with javascript asyc variables.

Cheers


Solution

  • This is the right answer. It took me a while to hack it as there is not much documentation on videoJS

    videojs("example_video_1", {}, function(){
        // Player (this) is initialized and ready.
        var aTextTrack =  this.textTracks()[0];
        aTextTrack.on('loaded', function() {
                console.log('here it is');
                cues = aTextTrack.cues();
                console.log('Ready State', aTextTrack.readyState()) 
                console.log('Cues', cues);
        });
    
        aTextTrack.show();//this method call triggers the subtitles to be loaded and loaded trigger
        this.play();
    
    });