@@ -2,8 +2,11 @@ import type { IBasePluginOptions } from 'xgplayer';
22import VideoJs from 'video.js' ;
33import { BasePlugin , Events } from 'xgplayer' ;
44
5+ type VideoSourceType = 'm3u8' | 'mp4' | 'hls' | 'dash' | 'rtmp' ;
6+
57class VideoJsPlugin extends BasePlugin {
68 url ?: string ;
9+ sourceType ?: VideoSourceType ;
710 videoPlayer ?: VideoJs . Player ;
811 static get pluginName ( ) {
912 return 'VideoJsPlugin' ;
@@ -19,16 +22,42 @@ class VideoJsPlugin extends BasePlugin {
1922 super ( args ) ;
2023 }
2124
25+ private getVideoJsMimeType ( type ?: VideoSourceType ) : string | undefined {
26+ switch ( type ) {
27+ case 'm3u8' :
28+ case 'hls' :
29+ return 'application/x-mpegURL' ;
30+ case 'mp4' :
31+ return 'video/mp4' ;
32+ case 'dash' :
33+ return 'application/dash+xml' ;
34+ default :
35+ return undefined ;
36+ }
37+ }
38+
39+ private loadSource ( url ?: string , type ?: VideoSourceType ) {
40+ if ( ! url )
41+ return ;
42+ const mimeType = this . getVideoJsMimeType ( type ) ;
43+ console . log ( 'videojs load url' , url , type ? `(type: ${ type } )` : '' ) ;
44+ if ( mimeType ) {
45+ this . videoPlayer ?. src ( {
46+ src : url ,
47+ type : mimeType ,
48+ } ) ;
49+ return ;
50+ }
51+ this . videoPlayer ?. src ( url ) ;
52+ }
53+
2254 beforePlayerInit ( ) {
2355 this . videoPlayer = VideoJs ( this . player . video as Element , {
2456 controlBar : false ,
2557 controls : false ,
2658 } ) ;
2759 this . videoPlayer . crossOrigin ( 'anonymous' ) ;
28- if ( this . url ) {
29- console . log ( 'videojs load url' , this . url ) ;
30- this . videoPlayer . src ( this . url ) ;
31- }
60+ this . loadSource ( this . url , this . sourceType ) ;
3261 }
3362
3463 afterPlayerInit ( ) {
@@ -38,10 +67,10 @@ class VideoJsPlugin extends BasePlugin {
3867 afterCreate ( ) {
3968 // 在afterCreate中可以加入DOM的事件监听
4069 this . url = this . player . config . url as string ;
70+ this . sourceType = ( this . player . config as any ) . videoType ;
4171 this . on ( Events . URL_CHANGE , ( url : string ) => {
42- console . log ( 'videojs load url' , url ) ;
4372 this . url = url ;
44- this . videoPlayer ?. src ( this . url ) ;
73+ this . loadSource ( this . url , this . sourceType ) ;
4574 } ) ;
4675 }
4776
0 commit comments