Skip to content

Commit e4fd42b

Browse files
committed
feat: 增加播放等事件回调
1 parent 9f9a224 commit e4fd42b

1 file changed

Lines changed: 62 additions & 45 deletions

File tree

src/components/BootstrapBlazor.Player/Player.razor.js

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export async function init(id, invoke, method, options) {
3636
...options
3737
}
3838
p.player = new Plyr(el, config);
39+
handlerEvents(p);
40+
3941
if (source.sources.length === 0) {
4042
return;
4143
}
@@ -62,10 +64,70 @@ const initHls = (p, options) => {
6264
setTimeout(() => hls.subtitleTrack = player.currentTrack, 50);
6365
});
6466
p.player = player;
67+
handlerEvents(p);
6568
});
6669
}
6770
}
6871

72+
export function reload(id, options) {
73+
const p = Data.get(id);
74+
if (p === null) {
75+
return;
76+
}
77+
78+
const { player, hls } = p;
79+
const source = options.source.sources;
80+
delete options.source;
81+
if (hls) {
82+
if (source.length > 0) {
83+
const src = source[0].src;
84+
hls.loadSource(src);
85+
}
86+
}
87+
else {
88+
player.poster = source.poster ?? options.poster;
89+
player.source = source;
90+
}
91+
}
92+
93+
export function setPoster(id, poster) {
94+
const p = Data.get(id);
95+
if (p) {
96+
const { player } = p;
97+
player.poster = poster;
98+
}
99+
}
100+
101+
export function dispose(id) {
102+
const p = Data.get(id);
103+
Data.remove(id);
104+
105+
if (p) {
106+
const { player } = p;
107+
if (player) {
108+
player.destroy();
109+
player = null;
110+
}
111+
}
112+
}
113+
114+
const handlerEventName = (name, p) => {
115+
const { el, invoke, method, player } = p;
116+
player.on(name, () => {
117+
const fire = el.getAttribute('data-bb-event') === 'true';
118+
if (fire) {
119+
console.log(name);
120+
invoke.invokeMethodAsync(method, name);
121+
}
122+
});
123+
}
124+
125+
const handlerEvents = p => {
126+
['ready', 'play', 'pause', 'ended', 'enterfullscreen', 'exitfullscreen', 'languagechange'].forEach(name => {
127+
handlerEventName(name, p);
128+
});
129+
}
130+
69131
const setLang = (option) => {
70132
option.i18n = {
71133
restart: '重启',
@@ -112,48 +174,3 @@ const setLang = (option) => {
112174
}
113175
}
114176
}
115-
116-
export function reload(id, options) {
117-
const p = Data.get(id);
118-
if (p === null) {
119-
return;
120-
}
121-
122-
const { player, hls } = p;
123-
const source = options.source.sources;
124-
delete options.source;
125-
if (hls) {
126-
if (source.length > 0) {
127-
const src = source[0].src;
128-
hls.loadSource(src);
129-
}
130-
}
131-
else {
132-
player.poster = source.poster ?? options.poster;
133-
player.source = source;
134-
}
135-
}
136-
137-
export function setPoster(id, poster) {
138-
execute(id, p => {
139-
const { player } = p;
140-
player.poster = poster;
141-
});
142-
}
143-
144-
const execute = (id, callback) => {
145-
const p = Data.get(id);
146-
if (p) {
147-
callback(p);
148-
}
149-
}
150-
151-
export function dispose(id) {
152-
const p = Data.get(id);
153-
Data.remove(id);
154-
155-
execute(id, player => {
156-
player.destroy();
157-
player = null;
158-
});
159-
}

0 commit comments

Comments
 (0)