Angular2 with video.js error no compatible source

2019-07-28 00:42发布

问题:

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

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()
    }
}

回答1:

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

Make sure you have installed npm install --save @types/videojs

Example:

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()
    }
}


回答2:

Looks like an issue with accessing the video, below code is working

<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="http://vjs.zencdn.net/v/oceans.mp4" type="video/mp4" />

        <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>

Check : https://github.com/mbalex99/ng2-videojs/blob/master/app/app.ts