ng-cw-v12
Version:
Angular UI Component Library
167 lines (162 loc) • 7.6 kB
JavaScript
import * as i0 from '@angular/core';
import { EventEmitter, Component, Output, Input, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Subject } from 'rxjs';
import * as i1 from '@angular/platform-browser';
class VideoEasyComponent {
constructor(eleRef, sanitizer) {
this.eleRef = eleRef;
this.sanitizer = sanitizer;
/** 返回播放状态 */
this.ncPlaying = new EventEmitter();
/** 返回播放器 */
this.ncLoaded = new EventEmitter();
/** 视频地址 */
this.ncUrl = '';
/** 超时 */
this.ncTimeout = 10;
/** 播放失败文案 */
this.ncErrorText = '播放失败';
/** 播放超时文案 */
this.ncTimeoutText = '播放超时,视频源无响应';
/** 静音 */
this._muted = true;
/** 自动播放 */
this._autoplay = false;
this.messageListener = null;
this.errorSubject = new Subject();
this.iframeElement = null;
this.playerController = null;
// 生成唯一ID
this.uniqueId = `video_easy_${Math.random().toString(36).substring(2, 11)}`;
// 监听消息
this.messageListener = (event) => {
if (event.data && event.data.type === 'iframe-message') {
const eventData = event.data;
//只处理当前视频iframe的消息
//防止一次用几个组件,其中一个iframe发消息,所有组件都收到
if (eventData.uniqueId != this.uniqueId) {
return;
}
this.errorSubject.next(eventData);
if (eventData.message == 'play') {
this.ncPlaying.emit(true);
}
else if (eventData.message == 'pause') {
this.ncPlaying.emit(false);
}
else if (eventData.message == 'loaded') {
this.playerController = {
pause: () => {
this.sendCommandToIframe('pause');
},
play: (url) => {
this.sendCommandToIframe('play', url);
},
setMute: (mute) => {
this.sendCommandToIframe('setMute', mute);
},
destroy: () => {
this.sendCommandToIframe('destroy');
}
};
this.ncLoaded.emit(this.playerController);
}
}
};
window.addEventListener('message', this.messageListener);
}
set ncMuted(value) {
this._muted = value !== null && value !== undefined && value !== false && value !== 'false';
}
get ncMuted() {
return this._muted;
}
set ncAutoplay(value) {
this._autoplay = value !== null && value !== undefined && value !== false && value !== 'false';
}
get ncAutoplay() {
return this._autoplay;
}
ngAfterViewInit() {
// 获取 iframe 元素
const iframe = this.eleRef.nativeElement.querySelector('iframe');
if (iframe) {
this.iframeElement = iframe;
}
}
ngOnChanges(changes) {
if (changes['ncUrl']) {
this.updateVideoUrl();
}
}
ngOnDestroy() {
this.sendCommandToIframe('destroy');
}
updateVideoUrl() {
const iframeUrl = `assets/easyplayer-js/viewer.html?url=${encodeURIComponent(this.ncUrl)}&autoplay=${this.ncAutoplay}&muted=${this.ncMuted}&uniqueId=${this.uniqueId}&timeout=${this.ncTimeout}&errorText=${this.ncErrorText}&timeoutText=${this.ncTimeoutText}`;
this.viewerUrl = this.sanitizer.bypassSecurityTrustResourceUrl(iframeUrl);
}
sendCommandToIframe(command, params) {
if (this.iframeElement && this.iframeElement.contentWindow) {
const message = {
type: 'player-command',
command: command,
params: params
};
this.iframeElement.contentWindow.postMessage(message, '*');
}
}
}
VideoEasyComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: VideoEasyComponent, deps: [{ token: i0.ElementRef }, { token: i1.DomSanitizer }], target: i0.ɵɵFactoryTarget.Component });
VideoEasyComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "12.1.5", type: VideoEasyComponent, selector: "nc-video-easy", inputs: { ncUrl: "ncUrl", ncTimeout: "ncTimeout", ncErrorText: "ncErrorText", ncTimeoutText: "ncTimeoutText", ncMuted: "ncMuted", ncAutoplay: "ncAutoplay" }, outputs: { ncPlaying: "ncPlaying", ncLoaded: "ncLoaded" }, usesOnChanges: true, ngImport: i0, template: "<div class=\"nc-video-easy-container\">\r\n <iframe [src]=\"viewerUrl\" width=\"100%\" height=\"100%\" [attr.frameBorder]=\"0\" style=\"border: none;\">\r\n </iframe>\r\n</div>", styles: [".nc-video-easy-container{position:relative;width:100%;height:100%;overflow:hidden}\n"] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: VideoEasyComponent, decorators: [{
type: Component,
args: [{
selector: 'nc-video-easy',
templateUrl: './video-easy.component.html',
styleUrls: ['./video-easy.component.less']
}]
}], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i1.DomSanitizer }]; }, propDecorators: { ncPlaying: [{
type: Output
}], ncLoaded: [{
type: Output
}], ncUrl: [{
type: Input
}], ncTimeout: [{
type: Input
}], ncErrorText: [{
type: Input
}], ncTimeoutText: [{
type: Input
}], ncMuted: [{
type: Input
}], ncAutoplay: [{
type: Input
}] } });
class NcVideoEasyModule {
}
NcVideoEasyModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcVideoEasyModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
NcVideoEasyModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcVideoEasyModule, declarations: [VideoEasyComponent], imports: [CommonModule], exports: [VideoEasyComponent] });
NcVideoEasyModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcVideoEasyModule, imports: [[
CommonModule
]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcVideoEasyModule, decorators: [{
type: NgModule,
args: [{
declarations: [
VideoEasyComponent
],
imports: [
CommonModule
],
exports: [
VideoEasyComponent
]
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { NcVideoEasyModule, VideoEasyComponent };
//# sourceMappingURL=ng-cw-v12-video-easy.js.map