在 video.js 中阅读视频 elements/attributes 的正确方法是什么?

What is the proper way to read the video elements/attributes in video.js?

我需要阅读并修改视频元素中的一些标签和属性,该元素使用 video.js 作为其播放器。我已经能够使用 getElementsByTagName 和 getAttribute 读取信息,如下面的代码所示。但我希望有一种 easier/better/best 方法可以通过 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);
}

谢谢。

当您使用 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'
});
// 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')