Skip to content
This repository has been archived by the owner on Jul 31, 2020. It is now read-only.

Commit

Permalink
srcObject support
Browse files Browse the repository at this point in the history
  • Loading branch information
buu700 committed Mar 22, 2020
1 parent f4c1b53 commit 88ff9af
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ A slightly more customized example, in your HTML file:
## API
Attribute | Type | Description | Default
--- | --- | --- | ---
*src* | **string** | Path or URL to a video | *null*
*src* | **string|MediaStream|MediaSource|Blob** | Path, URL, or `srcObject` for a video | *null*
*title* | **string** | Title for the video | *null*
*autoplay* | **boolean** | Whether the video should autoplay | *false*
*preload* | **boolean** | Whether the video should preload | *true*
Expand Down
2 changes: 1 addition & 1 deletion src/app/video/video.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{title}}
</div>

<video #video class="video" [attr.src]="src ? src : null" [attr.autoplay]="autoplay ? true : null"
<video #video class="video" [attr.autoplay]="autoplay ? true : null"
[preload]="preload ? 'auto' : 'metadata'" [attr.poster]="poster ? poster : null"
[attr.loop]="loop ? loop : null">
<ng-content select="source"></ng-content>
Expand Down
45 changes: 43 additions & 2 deletions src/app/video/video.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
ElementRef,
EventEmitter,
Input,
OnChanges,
OnDestroy,
Output,
Renderer2,
SimpleChanges,
ViewChild,
} from '@angular/core';
import { ThemePalette } from '@angular/material/core';
Expand All @@ -19,11 +21,11 @@ import { EventService } from './services/event.service';
templateUrl: './video.component.html',
styleUrls: ['./video.component.css', './styles/icons.css']
})
export class MatVideoComponent implements AfterViewInit, OnDestroy {
export class MatVideoComponent implements AfterViewInit, OnChanges, OnDestroy {
@ViewChild('player') private player: ElementRef;
@ViewChild('video') private video: ElementRef;

@Input() src: string = null;
@Input() src: string|MediaStream|MediaSource|Blob = null;
@Input() title: string = null;
@Input() autoplay: boolean = false;
@Input() preload: boolean = true;
Expand Down Expand Up @@ -83,6 +85,8 @@ export class MatVideoComponent implements AfterViewInit, OnDestroy {

private events: EventHandler[];

private srcObjectURL: string;

constructor(
private renderer: Renderer2,
private evt: EventService
Expand All @@ -101,12 +105,20 @@ export class MatVideoComponent implements AfterViewInit, OnDestroy {
this.video.nativeElement.onloadeddata = () => this.videoLoaded = true;

this.evt.addEvents(this.renderer, this.events);
this.setVideoSrc(this.src);
}

ngOnChanges(changes: SimpleChanges): void {
if (changes.src) {
this.setVideoSrc(this.src);
}
}

ngOnDestroy(): void {
this.video.nativeElement.onloadeddata = null;

this.evt.removeEvents(this.events);
this.setVideoSrc(null);
}

load(): void {
Expand Down Expand Up @@ -144,4 +156,33 @@ export class MatVideoComponent implements AfterViewInit, OnDestroy {
}
}

private setVideoSrc(src: string|MediaStream|MediaSource|Blob): void {
if (!this.video || !this.video.nativeElement) {
return;
}

this.video.nativeElement.src = null;
if ('srcObject' in HTMLVideoElement.prototype) {
this.video.nativeElement.srcObject = null;
}

if (this.srcObjectURL) {
URL.revokeObjectURL(this.srcObjectURL);
this.srcObjectURL = null;
}

if (!src) {
return;
}

if (typeof src === 'string') {
this.video.nativeElement.src = src;
} else if ('srcObject' in HTMLVideoElement.prototype) {
this.video.nativeElement.srcObject = src;
} else {
this.srcObjectURL = URL.createObjectURL(src);
this.video.nativeElement.src = this.srcObjectURL;
}
}

}

0 comments on commit 88ff9af

Please sign in to comment.