如果声明为对象的一部分,VideoJS将不会启动视频

时间:2016-01-23 19:55:38

标签: javascript video.js

我目前正在尝试使用VideoJS创建一个有多个直播的页面。为了使这更容易,我想使用类/对象来实例化和修改每个流。显然这不起作用,无论我做什么,VideoJS不能作为对象的一部分工作?如果是这样,是否有正确的解决方法或我做错了什么?

这是我的班级:

function Stream(id, info, container){
    this.id = id;
    this.paddedId = ("00000" + this.id).substr(-5,5);
    this.live = info['live'];
    this.onlineSince = info['onlineSince'];
    this.src_html5 = [{ type: 'application/dash+xml', src: 'https://piczel.tv:8083/stream_' + this.paddedId + '/stream_' + this.paddedId + '/manifest.mpd' },
                             { type: 'application/x-mpegURL', src: 'https://piczel.tv:8083/stream_' + this.paddedId + '/stream_' + this.paddedId + '/playlist.m3u8' }];
    this.player = videojs(container);
    this.viewers = info['viewers'];

    this.player.src(this.src_html5);

    // Create buttons on player
    this.viewerCount = this.player.controlBar.addChild('Button', { 'text': 'Viewers' }).addClass("vjs-viewers"),
    this.toggleFlashButton = this.player.controlBar.addChild('Button', { 'text': 'Flash' }).addClass('vjs-flash-toggle'),
    this.onlineSinceCount = this.player.controlBar.addChild('Button', { 'text': 'Online since' }).addClass('vjs-onlinesince'),

    //this.viewerCount.html('<i class="glyphicon glyphicon-eye-open"></i> <span class="vjs-current-viewers">' + this.viewers + '</span>');
    $('.vjs-flash-toggle').html('<span class="vjs-flash-toggle-text">HTML5</span>');

    if(this.live){
        this.player.ready(function(){
            this.play();
        });
    }

    this.getLive =  function() {
        return this.live;
    }

    /*this.addViewer = function() {
        this.viewers += 1;
    }*/

}

这是我实例化它的方式:

$(document).ready(function(){
{% for stream in streams %}
    streamInfo = {};
    streamInfo['live'] = {% if stream.live %}true{% else %}false{% endif %};
    streamInfo['viewers'] = {{ stream.viewers }};
    //streamInfo['src']['flash'] = { type: 'rtmp/mp4', src: 'rtmp://piczel.tv:1936/stream_{{ "%05d"|format(stream.id) }}/stream_{{ "%05d"|format(stream.id) }}' };
    streamInfo['onlineSince'] = new Date(now.getTime() - {{ stream.lastOnline ? stream.lastOnline ~ '*1000' : 'now.getTime()' }});

    var stream_{{ stream.id }} = new Stream({{ stream.id }}, streamInfo, 'stream_' + {{ stream.id }});
{% endfor %}
});

似乎可以加载直播,播放时我们会在出错时得到这个非常精确的错误:

[8660][streamController] Video Element Error: UNKNOWN 

JSBin:http://output.jsbin.com/kipifanoto/1

1 个答案:

答案 0 :(得分:1)

您的JSBin示例不起作用,因为该元素是div而不是video。将video元素与默认的video.js类和controls属性一起使用,以便生成控件,这样就可以了。

<video id="stream_01" class="video-js vjs-default-skin" controls></video>

如果您想创建一个播放器并将其插入DOM,您可以这样做:

function Stream(id, container) {
  this.id = id;
  this.player = videojs(document.createElement('video'), {controls: true});
  this.player.addClass('video-js').addClass('vjs-default-skin');
  document.getElementById(container).appendChild(this.player.el());
  ...