mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-26 08:51:20 -08:00
构建事件代理
This commit is contained in:
1 parent
f4c2e11b86
commit
b7e740ae61
6 files changed
+233
-13
No files matched your search
@@ -0,0 +1,33 @@
|
||||
export default {
|
||||
mediaSource: {
|
||||
propertys: ['activeSourceBuffers', 'duration', 'readyState', 'sourceBuffers'],
|
||||
methods: [
|
||||
'addSourceBuffer',
|
||||
'endOfStream',
|
||||
'removeSourceBuffer',
|
||||
'clearLiveSeekableRange',
|
||||
'setLiveSeekableRange',
|
||||
],
|
||||
events: ['sourceclose', 'sourceended', 'sourceopen'],
|
||||
},
|
||||
sourceBuffer: {
|
||||
propertys: [
|
||||
'mode',
|
||||
'updating',
|
||||
'buffered',
|
||||
'timestampOffset',
|
||||
'audioTracks',
|
||||
'videoTracks',
|
||||
'textTracks',
|
||||
'appendWindowStart',
|
||||
'appendWindowEnd',
|
||||
'trackDefaults',
|
||||
],
|
||||
methods: ['appendBuffer', 'appendStream', 'abort', 'remove'],
|
||||
events: ['abort', 'error', 'update', 'updateend', 'updatestart'],
|
||||
},
|
||||
sourceBufferList: {
|
||||
propertys: ['length'],
|
||||
events: ['addsourcebuffer', 'removesourcebuffer'],
|
||||
},
|
||||
};
|
||||
@@ -1,7 +1,34 @@
|
||||
import config from './config';
|
||||
|
||||
export default class CreatMediaSource {
|
||||
constructor(flv) {
|
||||
this.flv = flv;
|
||||
this.mediaSource = new MediaSource();
|
||||
this.url = URL.createObjectURL(this.mediaSource);
|
||||
flv.mediaElement.src = this.url;
|
||||
const url = URL.createObjectURL(this.mediaSource);
|
||||
flv.events.destroyEvents.push(() => {
|
||||
URL.revokeObjectURL(url);
|
||||
});
|
||||
flv.mediaElement.src = url;
|
||||
}
|
||||
}
|
||||
|
||||
eventBind() {
|
||||
const {
|
||||
events: { proxy },
|
||||
} = this.flv;
|
||||
|
||||
config.mediaSource.events.forEach(eventName => {
|
||||
proxy(this.mediaSource, eventName, event => {
|
||||
this.art.emit(`mediaSource:${event.type}`, event);
|
||||
});
|
||||
});
|
||||
|
||||
config.sourceBufferList.events.forEach(eventName => {
|
||||
proxy(this.mediaSource.sourceBuffers, eventName, event => {
|
||||
this.art.emit(`sourceBuffers:${event.type}`, event);
|
||||
});
|
||||
proxy(this.mediaSource.activeSourceBuffers, eventName, event => {
|
||||
this.art.emit(`activeSourceBuffers:${event.type}`, event);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
export default class EventProxy {
|
||||
constructor() {
|
||||
this.destroyEvents = [];
|
||||
this.proxy = this.proxy.bind(this);
|
||||
}
|
||||
|
||||
proxy(target, name, callback, option = {}) {
|
||||
target.addEventListener(name, callback, option);
|
||||
this.destroyEvents.push(() => {
|
||||
target.removeEventListener(name, callback, option);
|
||||
});
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.destroyEvents.forEach(event => event());
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,54 @@
|
||||
import Emitter from 'tiny-emitter';
|
||||
import CheckSupport from './checkSupport';
|
||||
import EventProxy from './eventProxy';
|
||||
import CreatMediaSource from './creatMediaSource';
|
||||
import * as utils from './utils';
|
||||
import config from './config';
|
||||
|
||||
let id = 0;
|
||||
class Flv extends Emitter {
|
||||
constructor(mediaElement, url) {
|
||||
super();
|
||||
utils.errorHandle(mediaElement instanceof HTMLVideoElement, 'The first parameter is not a video tag element');
|
||||
utils.errorHandle(typeof url === 'string', 'The second parameter is not a string type');
|
||||
this.mediaElement = mediaElement;
|
||||
this.url = url;
|
||||
|
||||
this.support = new CheckSupport(this);
|
||||
this.events = new EventProxy(this);
|
||||
this.mediaSource = new CreatMediaSource(this);
|
||||
|
||||
id += 1;
|
||||
this.id = id;
|
||||
Flv.instances.push(this);
|
||||
}
|
||||
|
||||
static get version() {
|
||||
return '__VERSION__';
|
||||
}
|
||||
|
||||
static get config() {
|
||||
return config;
|
||||
}
|
||||
|
||||
static get utils() {
|
||||
return utils;
|
||||
}
|
||||
|
||||
load() {
|
||||
console.log(this);
|
||||
console.log(this.id);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
console.log(this);
|
||||
this.events.destroy();
|
||||
Flv.instances.splice(Flv.instances.indexOf(this), 1);
|
||||
this.emit('destroy');
|
||||
}
|
||||
}
|
||||
|
||||
Object.defineProperty(Flv, 'instances', {
|
||||
value: [],
|
||||
});
|
||||
|
||||
window.Flv = Flv;
|
||||
export default Flv;
|
||||
Reference in new issue
Block a user