Skip to content

Commit 833f3cc

Browse files
authored
Refactor plyr (#422)
* chore: bump version 9.0.1 * feat: 增加 OnEvent 回调方法 * feat: 增加播放等事件回调 * chore: 精简代码
1 parent 06ac64a commit 833f3cc

4 files changed

Lines changed: 86 additions & 52 deletions

File tree

src/components/BootstrapBlazor.Player/BootstrapBlazor.Player.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.0.0</Version>
4+
<Version>9.0.1</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

src/components/BootstrapBlazor.Player/Player.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
@if (Mode == PlayerMode.Audio)
66
{
7-
<audio id="@Id" playsinline controls crossorigin>
7+
<audio id="@Id" playsinline controls crossorigin data-bb-event="@EventString">
88
</audio>
99
}
1010
else {
11-
<video id="@Id" playsinline controls crossorigin>
11+
<video id="@Id" playsinline controls crossorigin data-bb-event="@EventString">
1212
</video>
1313
}

src/components/BootstrapBlazor.Player/Player.razor.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ public partial class Player
2525
[EditorRequired]
2626
public PlayerOptions? Options { get; set; }
2727

28-
private string? ClassString => CssBuilder.Default("bb-video-player")
29-
.AddClassFromAttributes(AdditionalAttributes)
30-
.Build();
28+
/// <summary>
29+
/// Gets or sets the client event callback. Default is null.
30+
/// </summary>
31+
[Parameter]
32+
public Func<string, Task>? OnEvent { get; set; }
33+
34+
private string? EventString => OnEvent == null ? "true" : null;
3135

3236
/// <summary>
3337
/// <inheritdoc/>
@@ -39,7 +43,7 @@ protected override async Task InvokeInitAsync()
3943
{
4044
Options.Language ??= CultureInfo.CurrentUICulture.Name;
4145
}
42-
await InvokeVoidAsync("init", Id, Interop, "", Options);
46+
await InvokeVoidAsync("init", Id, Interop, nameof(TriggerEvent), Options);
4347
}
4448

4549
/// <summary>
@@ -48,4 +52,18 @@ protected override async Task InvokeInitAsync()
4852
/// <param name="option"></param>
4953
/// <returns></returns>
5054
public Task Reload(PlayerOptions option) => InvokeVoidAsync("reload", Id, option);
55+
56+
/// <summary>
57+
/// Trigger <see cref="OnEvent"/> event callback. Triggered by JSInterop.
58+
/// </summary>
59+
/// <param name="eventName"></param>
60+
/// <returns></returns>
61+
[JSInvokable]
62+
public async Task TriggerEvent(string eventName)
63+
{
64+
if (OnEvent != null)
65+
{
66+
await OnEvent(eventName);
67+
}
68+
}
5169
}

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

Lines changed: 61 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,69 @@ 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+
invoke.invokeMethodAsync(method, name);
120+
}
121+
});
122+
}
123+
124+
const handlerEvents = p => {
125+
['ready', 'play', 'pause', 'ended', 'enterfullscreen', 'exitfullscreen', 'languagechange'].forEach(name => {
126+
handlerEventName(name, p);
127+
});
128+
}
129+
69130
const setLang = (option) => {
70131
option.i18n = {
71132
restart: '重启',
@@ -112,48 +173,3 @@ const setLang = (option) => {
112173
}
113174
}
114175
}
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)