Skip to content

Commit d4f671c

Browse files
committed
feat: add local permission for volume and muted
1 parent b525d45 commit d4f671c

File tree

4 files changed

+17
-58
lines changed

4 files changed

+17
-58
lines changed

packages/app-plyr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@netless/app-plyr",
3-
"version": "0.2.6-beta.6",
3+
"version": "0.2.6-beta.7",
44
"description": "Netless App Media Player, based on plyr.io.",
55
"repository": "netless-io/netless-app",
66
"license": "MIT",

packages/app-plyr/playground.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const options: PlaygroundConfigs<Attributes> = [
1414
type: "video/mp4",
1515
useNewPlayer: true,
1616
paused: false,
17+
// syncVolume: false,
18+
// syncMuted: false,
1719
},
1820
},
1921
{

packages/app-plyr/src/controller.ts

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class Controller {
5454

5555
public constructor(context: AppContext<Attributes>) {
5656
this.context = context;
57-
const { src, provider, type, paused, poster } = this.context.storage.state;
57+
const { src, provider, type, poster } = this.context.storage.state;
5858
const _type = provider ? undefined : type || guessTypeFromSrc(src);
5959
this.playerContainer = this.createPlayerContainer({ src, poster, provider, type: _type });
6060
(window as any).plyrController = this;
@@ -143,9 +143,14 @@ export class Controller {
143143
get duration(): number {
144144
return this.player?.duration || 0;
145145
}
146-
147146
private hasPermission = (_operation: PlayerOperationType):PermissionType => {
148147
// todo 如果客户需要更细粒度的权限控制,可以在这里添加
148+
if (_operation === 'volume' && !this.context.storage.state.syncVolume) {
149+
return 'local';
150+
}
151+
if (_operation === 'muted' && !this.context.storage.state.syncMuted) {
152+
return 'local';
153+
}
149154
if (this.context.getIsWritable()) {
150155
return 'sync';
151156
}
@@ -165,10 +170,10 @@ export class Controller {
165170
muted?: boolean;
166171
playTimeState?: PlayTimeState;
167172
} = {};
168-
if (this.player.volume !== this.volumeData) {
173+
if (this.player.volume !== this.volumeData && this.context.storage.state.syncVolume) {
169174
willUpdateAttr.volume = this.volumeData;
170175
}
171-
if (this.player.muted !== this.mutedData) {
176+
if (this.player.muted !== this.mutedData && this.context.storage.state.syncMuted) {
172177
willUpdateAttr.muted = this.mutedData;
173178
}
174179
const playTimeState = this.playTimeState;
@@ -269,7 +274,6 @@ export class Controller {
269274
try {
270275
loop++;
271276
await this.player.play();
272-
// this.checkPlayMuted();
273277
} catch (error) {
274278
console.error('[app plyr] play error', error);
275279
if (this.player) {
@@ -280,57 +284,6 @@ export class Controller {
280284
}
281285
}
282286

283-
// private checkPlayMuted = () => {
284-
// setTimeout(async () => {
285-
// if (!this.player) {
286-
// return;
287-
// }
288-
// if (this.player.muted !== this.mutedData) {
289-
// const mutedDom = this.player.elements.container?.querySelector('.plyr__volume button') as HTMLButtonElement;
290-
// // mutedDom.click();
291-
// // try {
292-
// // const stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false })
293-
// // console.log("设备权限已获取,自动播放限制可能已解除");
294-
// // stream.getTracks().forEach(track => track.stop());
295-
// // } catch (error) {
296-
// // console.log("权限请求失败", error);
297-
// // }
298-
// // 在页面初始化时,尝试请求用户媒体权限
299-
300-
// // .then(function(stream) {
301-
// // // 权限获取成功!此时浏览器的自动播放策略可能会放宽
302-
// // console.log("设备权限已获取,自动播放限制可能已解除");
303-
304-
// // // 注意:这里我们并不真正使用这个stream,目的是获取权限
305-
// // // 关闭获取到的媒体轨道
306-
// // stream.getTracks().forEach(track => track.stop());
307-
308-
// // // 现在尝试播放你的背景音乐
309-
// // const audio = new Audio('your-audio.mp3');
310-
// // audio.play().catch(e => console.error("最终还是失败了:", e));
311-
// // })
312-
// // .catch(function(err) {
313-
// // // 用户拒绝了权限请求或发生错误,自动播放依然会被阻止
314-
// // console.log("权限请求失败", err);
315-
// // // 此时需要降级到方案一,引导用户交互
316-
// // });
317-
318-
// // mutedDom.addEventListener('pointerdown', (e)=>{
319-
// // console.log('[app plyr] checkPlayMuted pointerdown===>', e);
320-
// // });
321-
// // mutedDom.click();
322-
// // 模拟一个pointerdown事件
323-
// // const pointerdownEvent = new PointerEvent('pointerdown', {
324-
// // bubbles: true,
325-
// // cancelable: true,
326-
// // composed: true,
327-
// // });
328-
// // console.log('[app plyr] checkPlayMuted====>', mutedDom, pointerdownEvent);
329-
// // mutedDom.dispatchEvent(pointerdownEvent);
330-
// }
331-
// }, 1000);
332-
// }
333-
334287
private createYoutubeContainer(src: string, poster?: string): HTMLDivElement {
335288
const container = document.createElement('div');
336289
container.classList.add('plyr__video-embed');

packages/app-plyr/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,22 @@ export interface Attributes {
2626
provider?: "youtube" | "vimeo";
2727
owner?: string;
2828
iconUrl?: string;
29+
syncVolume?: boolean; // 是否同步音量,默认同步
30+
syncMuted?: boolean; // 是否同步静音,默认同步
2931
}
3032

3133
export interface AppResult {
3234
controller?: Controller;
3335
}
3436

35-
const DefaultAttributes: Pick<Attributes, "volume" | "paused" | "muted" | "currentTime" | "useNewPlayer"> = {
37+
const DefaultAttributes: Pick<Attributes, "volume" | "paused" | "muted" | "currentTime" | "useNewPlayer" | "syncVolume" | "syncMuted"> = {
3638
volume: 1,
3739
paused: true,
3840
muted: false,
3941
currentTime: 0,
4042
useNewPlayer: false,
43+
syncVolume: true,
44+
syncMuted: true,
4145
};
4246

4347
const Plyr: NetlessApp<Attributes, any, any, AppResult> = {

0 commit comments

Comments
 (0)