阅读video.js中视频元素/属性的正确方法是什么?

时间:2016-02-06 06:24:45

标签: javascript video video.js getelementsbytagname getattribute

我需要阅读并修改视频元素中的一些标记和属性,视频元素使用video.js作为播放器。我已经能够使用getElementsByTagName和getAttribute读取信息,如下面的代码所示。但我希望通过video.js(5.6.0)有更简单/更好/最好的方法来实现这一点。

var video = document.getElementsByTagName('video')[0];
var poster = video.getAttribute('poster');
var sources = video.getElementsByTagName('source');
var tracks = video.getElementsByTagName('track');

if (poster != null)
{
    var newposter = addChk(poster, "p");  // generates modified source
    video.setAttribute('poster', newposter);
}

for (var i = 0; i < sources.length; i++) {
    var s = sources[i].getAttribute('src');
    s = addChk(s,"f");                    // generates modified source
    sources[i].setAttribute('src', s);
}

for (var i = 0; i < tracks.length; i++)
{
    var s = tracks[i].getAttribute('src');
    s = addChk(s, "c");                   // generates modified source
    tracks[i].setAttribute('src', s);
}

感谢。

2 个答案:

答案 0 :(得分:1)

当你使用video.js时,你应该使用它的API。在创建播放器后直接修改视频元素并不总是按预期工作,如果您使用不同的播放技术(例如IE8上的Flash),则甚至不会有视频元素。

var player = videojs('my_player_id');

// Get/set poster:
console.log(player.poster());
player.poster('//example.com/poster.jpg');

// Get source:
console.log(player.currentSrc());

// Update source:
player.src({src: '//example.com/video.mp4', type: 'video/mp4'});

// Multiple sources:
player.src([
  {src: '//example.com/video.m3u8', type: 'application/x-mpegURL'},
  {src: '//example.com/video.mp4', type: 'video/mp4'}
]);

要更新曲目,您需要在添加for now之前删除现有曲目。

// Remove tracks:
var tracks = player.textTracks();
for (i = 0; i<tracks.length;i++) {
  player.removeRemoteTextTrack(tracks[i]);
}
// Add a track
player.addRemoteTextTrack({
  kind: 'captions',
  src: '//example.com/captions.vtt',
  srclang: 'is',
  label: 'íslenska'
});

答案 1 :(得分:0)

// never touch videoElement again. It's owned by videojs now.
var player = videojs(videoElement);

// IWillNotUseThisInPlugins is mandatory, per videojs spec
// https://github.com/videojs/video.js/issues/2617
player.tech({IWillNotUseThisInPlugins: true}).getAttribute('playsinline')