Angular2与video.js错误没有兼容的源 [英] Angular2 with video.js error no compatible source

查看:56
本文介绍了Angular2与video.js错误没有兼容的源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VIDEOJS:错误:(代码:4 MEDIA_ERR_SRC_NOT_SUPPORTED)找不到与此视频兼容的信号源.

VIDEOJS: ERROR: (CODE:4 MEDIA_ERR_SRC_NOT_SUPPORTED) No compatible source was found for this video.

我正在尝试在angular 2项目中播放视频.这是我的代码:

I am trying to play video in angular 2 project. Here is my code:

import {Component, ElementRef, OnInit, OnDestroy} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: `

    <video id="example_video_1" class="video-js vjs-default-skin"
        controls preload="auto" width="640" height="264"
        poster="http://video-js.zencoder.com/oceans-clip.png"
        data-setup='{"example_option":true}'>
        <source src="C:/Users/knare/Downloads/Joey Montana - Picky.mp4" type="video/mp4" />
        <source src="C:/Users/knare/Downloads/Joey Montana - Picky.webm" type="video/webm" />
        <source src="C:/Users/knare/Downloads/Joey Montana - Picky.ogv" type="video/ogg" />
        <p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
    </video>

    `
})
export class AppComponent implements OnInit, OnDestroy {

    private _elementRef: ElementRef
    private videoJSplayer : VideoJSPlayer

    constructor(elementRef: ElementRef) {
        this._elementRef = elementRef
    }

    ngOnInit() {
        var player = videojs('example_video_1', { /* Options */ }, function() {
              console.log('Good to go!');

              this.play(); // if you don't trust autoplay for some reason 

              // How about an event listener? 
              this.on('ended', function() {
                console.log('awww...over so soon?');
              });
            });
    }

    ngOnDestroy() {
        console.log('Deinit - Destroyed Component')
        this.videoJSplayer.dispose()
    }
}

推荐答案

您应该使用 ngAfterViewInit(),而不是 ngOnInit(),这是在视图被调用之前调用的已初始化,因此videojs没有可供选择的元素.

You should be using ngAfterViewInit() and not ngOnInit() this is called before the view is initialized so videojs has no element to select.

确保已安装 npm install --save @ types/videojs

示例:

import { AfterViewInit, Component, ElementRef, OnInit, OnDestroy, ViewChild } from 'angular2/core';


@Component({
    selector: 'my-app',
    template: `

    <video #video id="example_video_1" class="video-js vjs-default-skin"
        controls preload="auto" width="640" height="264"
        poster="http://video-js.zencoder.com/oceans-clip.png"
        data-setup='{"example_option":true}'>
        <source src="C:/Users/knare/Downloads/Joey Montana - Picky.mp4" type="video/mp4" />
        <source src="C:/Users/knare/Downloads/Joey Montana - Picky.webm" type="video/webm" />
        <source src="C:/Users/knare/Downloads/Joey Montana - Picky.ogv" type="video/ogg" />
        <p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
    </video>

    `
})
export class AppComponent implements AfterViewInit, OnInit, OnDestroy {
    @ViewChild() video;
    private _elementRef: ElementRef
    private videoJSplayer : VideoJSPlayer

    constructor(elementRef: ElementRef) {
        this._elementRef = elementRef
    }

    AfterViewInit() {
        var player = videojs(this.video.nativeElement, { /* Options */ }, function() {
              console.log('Good to go!');

              this.play(); // if you don't trust autoplay for some reason 

              // How about an event listener? 
              this.on('ended', function() {
                console.log('awww...over so soon?');
              });
            });
    }

    ngOnDestroy() {
        console.log('Deinit - Destroyed Component')
        this.videoJSplayer.dispose()
    }
}

这篇关于Angular2与video.js错误没有兼容的源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆