From 88479319723329e83f9aa3765403c437b103072f Mon Sep 17 00:00:00 2001 From: jexjws Date: Fri, 26 Jul 2024 12:01:33 +0000 Subject: [PATCH 1/3] New extension: Media Session --- extensions/40codeTeam/MediaSessApi.js | 328 ++++++++++++++++++++++++++ extensions/extensions.json | 1 + images/40codeTeam/MediaSessApi.png | Bin 0 -> 51489 bytes images/README.md | 4 + website/10sec_silent_audio.opus | Bin 0 -> 2485 bytes 5 files changed, 333 insertions(+) create mode 100644 extensions/40codeTeam/MediaSessApi.js create mode 100644 images/40codeTeam/MediaSessApi.png create mode 100644 website/10sec_silent_audio.opus diff --git a/extensions/40codeTeam/MediaSessApi.js b/extensions/40codeTeam/MediaSessApi.js new file mode 100644 index 0000000000..4d7fc686cb --- /dev/null +++ b/extensions/40codeTeam/MediaSessApi.js @@ -0,0 +1,328 @@ +// Name: +// ID: 40codeMediaSessApi +// Description: The SC block form of the Media Session API , allows SC works to basically use the browser Media Session ability. +// By: jexjws +// License: MIT + + +(function (Scratch) { + 'use strict'; + if (!Scratch.extensions.unsandboxed) { + throw new Error('This extension must run unsandboxed./此拓展需要以非沙盒模式运行。'); + } + class MyExtension { + /** + * 判断 mediaSession 是否可用 + * @returns {boolean} + */ + canIUse() { + return ("mediaSession" in navigator) + } + mediaSession_reset() { + if (!this.canIUse()) { return } + navigator.mediaSession.metadata = new MediaMetadata() + navigator.mediaSession.setPositionState(null); + } + + + actived_actionhandler = [] + actions = ["previoustrack", "nexttrack", "stop", 'seekbackward', 'seekforward', 'play', 'pause'] + /** + * mediaSession ActionHandler 维护者 + * @param {String} mode 要对事件做什么 + * @param {String} action 对哪个事件做 + * @returns {any} + */ + actionHandlerMan(mode, action = "") { + if (!this.canIUse()) { return } + action = String(action) + //console.log("actionHandlerMan",mode,action) + const setActionHandler = (...args) => navigator.mediaSession.setActionHandler(...args); + try { + switch (mode) { + case "reset": + this.actions.forEach((item) => (setActionHandler(item, null))) + this.actived_actionhandler = [] + break; + case "on": + if (!this.actived_actionhandler.includes(action)) { + setActionHandler(action, () => (Scratch.vm.runtime.startHats('40codeMediaSessApi_v1_actionhandler', { action: action }))) + this.actived_actionhandler.push(action) + } + break; + case "off": + if (this.actived_actionhandler.includes(action)) { + setActionHandler(action, null) + this.actived_actionhandler = this.actived_actionhandler.filter((item) => !(item === action)) + } + break; + default: + break; + } + } catch (error) { + console.error(error) + } + } + + constructor(runtime) { + Scratch.translate.setup({ + "zh-cn": { + "40code.MediaSessApi.v1.canIUse": 'mediaSession 是否可用', + "40code.MediaSessApi.v1.set": "设定当前播放媒体 [key] 为 [value]", + "40code.MediaSessApi.v1.set.menus.title": "标题", + "40code.MediaSessApi.v1.set.menus.artist": "艺术家名称", + "40code.MediaSessApi.v1.set.menus.album": "专辑名称", + "40code.MediaSessApi.v1.set_artwork": "设定当前播放媒体 封面图链接 为 [value]", + "40code.MediaSessApi.v1.set_position": "设定当前播放进度:总时长:[duration]秒、当前播放到:[position]秒、播放速率:[playbackRate]倍", + "40code.MediaSessApi.v1.unset": "清除所有设定", + + "40code.MediaSessApi.v1.set_actionhandler": "[on_off] mediaSession [action]事件接收帽子块", + "40code.MediaSessApi.v1.set_actionhandler.on": "启用", + "40code.MediaSessApi.v1.set_actionhandler.off": "禁用", + "40code.MediaSessApi.v1.set_actionhandler_status": "已启用的 mediaSession 事件接收帽子块", + "40code.MediaSessApi.v1.set_actionhandler_reset": "禁用所有 mediaSession 事件接收帽子块", + "40code.MediaSessApi.v1.actionhandler": "用户控制事件[action]发生时", + + "40code.MediaSessApi.v1.actionhandler.menus.nexttrack": "下一首", + "40code.MediaSessApi.v1.actionhandler.menus.previoustrack": "上一首", + "40code.MediaSessApi.v1.actionhandler.menus.stop": "停止", + "40code.MediaSessApi.v1.actionhandler.menus.play": "播放", + "40code.MediaSessApi.v1.actionhandler.menus.pause": "暂停", + "40code.MediaSessApi.v1.actionhandler.menus.seekbackward": "快退", + "40code.MediaSessApi.v1.actionhandler.menus.seekforward": "快进", + + "40code.MediaSessApi.v1.compatibility": "兼容性:使用播放无声音频的