Skip to content

🔧 API 总览

ctx 对象是插件的核心入口。本文档按模块列出所有插件可用的 API 速查表,每个 API 标注了所属的详情文档以便深入了解。

ℹ️ 本文档以 EchoMusicPlugins 仓库 官方文档为准。内部 API(FFmpeg 引擎、桌面歌词、Mini 播放器等)不在插件范围内,详见 内部 API 参考 →


🏗️ 宿主运行时

API类型说明
ctx.vueVue 3 运行时defineComponent / h / ref / computed / watch / onMounted / onUnmounted / defineAsyncComponent / reactive / resolveComponent
ctx.appApp 实例主应用 Vue App 实例
ctx.routerRouter 实例Vue Router 实例
ctx.piniaPinia 实例Pinia 状态管理实例

⚠️ 不能 import { ref } from 'vue',必须通过 ctx.vue.ref() 访问。


📦 状态管理(Stores)

API说明详见
ctx.stores.player播放状态:当前歌曲、进度、音量、播放模式播放器
ctx.stores.playlist播放队列状态播放器
ctx.stores.lyric歌词状态:当前行、翻译、偏移播放器
ctx.stores.settings用户全局设置
ctx.stores.theme主题色、深浅模式文件存储

🎨 UI 扩展

API说明详见
ctx.ui.addPage({id, title, icon?, component, sidebar?})注册侧边栏独立页面UI 扩展
ctx.ui.sidebar.addItem({id, title, icon?, pageId, section?, order?})为已注册页面添加侧边栏项UI 扩展
ctx.ui.settings.define({title, component})注册插件设置面板UI 扩展
ctx.ui.addSongContextMenuItem({id, label, onSelect})歌曲右键菜单项UI 扩展
ctx.ui.cover.setFallback({id, resolveUrl})封面无法加载时的回退图片UI 扩展
ctx.ui.mount(target, component, opts?)挂载 Vue 组件到 DOMUI 扩展
ctx.ui.teleport(component, opts?)Teleport 组件到 bodyUI 扩展
ctx.ui.components.Button / Switch / Input / Select / Slider预置 UI 控件(defineAsyncComponent 加载)UI 扩展
ctx.css.inject(css, {id?})动态注入 CSSUI 扩展
ctx.css.remove(id)移除注入的 CSSUI 扩展
ctx.dom.observe(selector, callback)监听 DOM 节点出现UI 扩展

🎵 播放 & 音频

播放控制

API说明详见
ctx.player.play()开始/恢复播放播放器
ctx.player.pause()暂停播放器
ctx.player.toggle()播放/暂停切换播放器
ctx.player.prev()上一首播放器
ctx.player.next()下一首播放器
ctx.player.stop()停止播放器
ctx.player.seek(time)跳转(秒)播放器
ctx.player.setVolume(v)设置音量(0-100)播放器
ctx.player.setPlaybackRate(r)倍速(0.5-3.0)播放器
ctx.player.setPlayMode(m)播放模式播放器
ctx.player.setAudioQuality(q)音频品质播放器
ctx.player.setAudioEffect(e)设置音效播放器
ctx.player.playTrack(track)播放指定歌曲播放器
ctx.player.playNext()播放下一首(跳过队列)播放器
ctx.player.playLast()播放上一首播放器
ctx.player.replaceQueueAndPlay(items)替换队列并播放播放器
ctx.player.dislikePersonalFm()私人 FM 不喜欢播放器
ctx.player.toggleLyricView(open?)切换歌词视图播放器

播放状态(computed,只读响应式)

属性类型说明
currentTrackcomputed当前歌曲对象
currentTrackIdcomputed当前歌曲 ID
currentTimecomputed播放进度(秒)
durationcomputed总时长(秒)
isPlayingcomputed是否播放中
playbackRatecomputed播放速率
volumecomputed音量(0-100)
playModecomputed播放模式

播放队列

API说明详见
ctx.playlist.getQueue()获取当前队列播放器
ctx.playlist.replaceQueue(items)替换整个队列播放器
ctx.playlist.addTrack(track)添加歌曲播放器
ctx.playlist.removeTrack(id)移除歌曲播放器
ctx.playlist.clear()清空队列播放器
ctx.playlist.reorder(from, to)重排播放器
ctx.playlist.playNext(track)插播(下一首播放)播放器

音频频谱

需要 capability:audioSpectrum: true

API说明详见
ctx.audio.spectrum.getStatus()获取捕获状态频谱
ctx.audio.spectrum.getSnapshot()单帧频谱快照频谱
ctx.audio.spectrum.subscribe(opts, cb)实时订阅频谱频谱

自定义音源 & 歌词

API说明详见
ctx.player.audioSource.register(opts)注册音源解析器(需 audioSource: true播放器
ctx.lyrics.registerResolver(opts)注册歌词解析器(需 lyrics: true播放器
ctx.lyrics.getSnapshot()歌词状态快照播放器
ctx.lyrics.onSnapshot(fn)订阅歌词变化播放器
ctx.lyrics.command(cmd)发送歌词命令播放器
ctx.lyricEffects.register(opts)注册歌词动效(需 lyricEffects: true播放器

ℹ️ FFmpeg 引擎高级控制(EQ/空间音效/IR/设备/渐变等)属于内部 API,不在插件范围内。详见 内部 API 参考 →

🌐 本地 HTTP 服务

需要 capability:webServer: true

API说明
ctx.webServer.listen(handler, opts?)启动 HTTP 服务,默认监听 127.0.0.1,返回端口号
ctx.webServer.status()获取当前服务状态
ctx.webServer.close()关闭服务
ctx.webServer.onRequest(handler)注册请求处理器

适合 Wallpaper Engine、OBS、本地脚本等桌面软件读取 EchoMusic 当前状态和歌词页面。插件禁用/卸载/安全模式/退出时自动释放端口。

🖥️ 桌面歌词

桌面歌词为独立窗口,与主窗口不共享 JS 内存。需同时声明 runtime.desktopLyric: true

API说明
ctx.desktopLyric桌面歌词窗口上下文,用于判断当前运行环境
ctx.lyricEffects.register({ scope: "desktop", ... })注册桌面歌词视觉效果(需 lyricEffects: true

💾 数据 & 文件

KV 存储

ctx.storage 提供按插件隔离的持久化 KV 存储。

API说明详见
ctx.storage.get(key)读取键值数据
ctx.storage.set(key, value)写入键值数据

插件卸载时 KV 数据自动清除;升级(覆盖安装)时保留。

文件系统

需要 capability:localFiles: true(大部分操作)

API说明详见
ctx.fs.listFiles(directory, opts?)扫描目录数据
ctx.fs.listImageFiles(directory, opts?)扫描图片文件数据
ctx.fs.readTextFile(path, opts?)读取文本文件数据
ctx.fs.readFileBytes(path, opts?)读取二进制文件数据
ctx.fs.writeFile(path, data, opts?)写入文件(仅插件目录内)数据
ctx.fs.deleteFile(path)删除文件(仅插件目录内)数据
ctx.fs.getFileUrl(path)获取文件播放 URL数据

外观 & 字体

API说明详见
ctx.appearance.getSnapshot()外观快照(主题色/深色模式/字体)数据
ctx.appearance.onSnapshot(fn)订阅外观变化数据
ctx.fonts.getAll()系统字体名称列表
ctx.fonts.getOptions({includeFollow?})字体选项(可用于 Select 组件)
ctx.fonts.buildFamily(name)构建 CSS font-family 字符串

SQLite 数据库

需要 capability:sqlite: true

插件私有 SQLite 数据库,按插件 id 隔离,由宿主托管。

API说明详见
ctx.sqlite.open({name?, migrations?})打开/创建数据库,默认库名 main数据
ctx.sqlite.listDatabases()列出所有数据库数据
ctx.sqlite.deleteDatabase(name?)删除指定数据库数据
db.exec(sql)执行 SQL数据
db.run(sql, params?)执行 INSERT/UPDATE/DELETE数据
db.get(sql, params?)查询单行数据
db.all(sql, params?)查询多行数据
db.transaction(fn)事务执行数据
db.close()关闭连接数据

支持 BLOB 参数,写入 { type: "hex", data }{ type: "base64", data },查询返回 hex 字符串。


🪟 窗口 & 系统

插件浮窗

浮窗需在 manifest contributes.windows 中声明。

API说明详见
ctx.windows.show()显示浮窗系统
ctx.windows.hide()隐藏浮窗系统
ctx.windows.close()关闭浮窗系统
ctx.windows.move(x, y)移动浮窗系统
ctx.windows.getBounds()获取浮窗位置和尺寸系统
ctx.windows.setIgnoreMouseEvents(bool)鼠标穿透系统

Now Playing

API说明详见
ctx.nowPlaying.getSnapshot()当前状态快照系统
ctx.nowPlaying.onSnapshot(fn)订阅状态变化系统
ctx.nowPlaying.onCommand(fn)监听命令系统
ctx.nowPlaying.syncPlayback(state)同步播放状态系统
ctx.nowPlaying.syncLyric(state)同步歌词状态系统

进程管理

需要 capability:process: true

API说明详见
ctx.process.launch(opts)启动插件目录内程序系统
ctx.process.terminate(pid?)终止已启动的进程系统

应用图标

API说明详见
ctx.appIcons.refresh()刷新所有图标缓存系统
ctx.appIcons.setRuntimeWindowIcon(path)设置运行时窗口图标系统
ctx.appIcons.restoreDefaultWindowIcon()恢复默认窗口图标系统
ctx.appIcons.restoreDefaultDesktopIcon()恢复默认桌面图标系统
ctx.appIcons.restoreDefaultTaskbarIcon()恢复默认任务栏图标系统

图标库

API说明
ctx.iconsIconify 图标对象,如 ctx.icons.iconPictureInPicture,可直接传入 Icon 组件
js
const Icon = ctx.vue.resolveComponent("Icon");
h(Icon, { icon: ctx.icons.iconPictureInPicture, width: 16, height: 16 });

系统对话框

API说明
ctx.dialog.selectDirectory(opts?)打开目录选择对话框,返回 {canceled, paths}
ctx.dialog.selectFiles(opts?)打开文件选择对话框,支持 multiplefilters

字体

API说明
ctx.fonts.getAll()系统可用字体名称列表
ctx.fonts.getOptions({includeFollow?})字体下拉选项
ctx.fonts.buildFamily(name)构建 CSS font-family

🎨 主题 & 样式

表面样式

js
ctx.theme.surface.set({
  enabled: true,
  mainOpacity: 82,         // 0-100
  sidebarOpacity: 82,
  cardOpacity: 86,
  playerOpacity: 92,
  backdropFilter: "blur(10px)",
  playerBackdropFilter: "blur(20px) saturate(180%)",
});
ctx.theme.surface.clear(); // 恢复默认

页面过渡动画

js
ctx.theme.pageTransition.set({
  enabled: true,
  mode: "out-in",
  appear: true,
  durationMs: 450,
  enterOpacity: 0,
  leaveOpacity: 0,
  enterTranslateY: 6,
});
ctx.theme.pageTransition.clear();

主题色渐变

js
ctx.theme.accentGradient.set({
  color: "#ff5c8a",
  angle: 180,
  height: "46%",
  peakOpacity: 0.28,
});
ctx.theme.accentGradient.clear();

📜 滚动画布

ctx.scroll 提供页面滚动容器的高级控制,适合滚动增强类插件。

API说明
queryContainers()获取所有滚动容器
getCurrentContainer()获取当前焦点容器
getState(el?)获取滚动状态
scrollToTop(el?)滚动到顶部
scrollToBottom(el?)滚动到底部
observeContainers(handler)监听滚动容器变化

🌐 酷狗 API

需要 capability:kugouApi: true

ctx.kugou 提供对 EchoMusic 内置酷狗音乐接口的访问。API 采用延迟加载,按需调用。

命名空间示例
ctx.kugou.musicctx.kugou.music.getSongUrl(hash)
ctx.kugou.userctx.kugou.user.getUserDetail()
ctx.kugou.playlistctx.kugou.playlist.getUserPlaylists()
ctx.kugou.videoctx.kugou.video.getVideoDetail(id)
ctx.kugou.searchctx.kugou.search.search(keyword)
ctx.kugou.artistctx.kugou.artist.getArtistDetail(id)
ctx.kugou.albumctx.kugou.album.getAlbumDetail(id)
ctx.kugou.commentctx.kugou.comment.getMusicComments(mixSongId)

⚠️ 插件仅传业务参数(hashid 等),不传敏感凭证(token、dfid、mid)。主进程自动注入当前登录态。

🔐 酷狗安全验证

需要 capability:kugouVerification: true

API说明
ctx.kugouVerification.request(challenge)处理酷狗安全验证挑战,返回 { ok: true, eventId }{ ok: false, error, canceled? }

⚡ 原生 API

Electron 对象

js
ctx.electron.platform  // "win32" | "darwin" | "linux"

可直接访问宿主 Electron API(如 ctx.electron.miniPlayer?.show()),但大部分场景应使用 ctx 封装的 API。


📋 插件管理

API说明详见
ctx.id当前插件 ID(只读)
ctx.manifest当前插件 manifest.json(只读)Manifest
ctx.dispose(fn)注册清理函数(插件禁用时调用)概览
ctx.toast.info(msg) / success(msg) / warning(msg) / danger(msg)提示消息
ctx.plugins.list()列出所有插件发布
ctx.plugins.installLocal(paths, opts?)本地安装发布
ctx.plugins.uninstall(pluginId)卸载插件发布
ctx.plugins.setEnabled(id, enabled)启用/禁用发布
ctx.plugins.setSafeMode(enabled)全局安全模式发布
ctx.plugins.reportFailure(failure)上报故障发布
ctx.plugins.clearFailure(pluginId?)清除故障记录发布
ctx.plugins.marketplace.listSources()列出插件源发布
ctx.plugins.marketplace.addSource(input)添加插件源发布
ctx.plugins.marketplace.patchSource(id, patch)修改源配置发布
ctx.plugins.marketplace.removeSource(id)移除插件源发布
ctx.plugins.marketplace.list(opts?)浏览在线插件发布
ctx.plugins.marketplace.install(srcId, pId, opts?)在线安装发布

下一步

文档内容
UI 扩展指南 →界面定制的深入实践
播放器与音频引擎 →播放控制、播放队列、频谱、歌词系统
文件存储与数据 →文件系统、KV 存储、外观订阅
音频频谱 →实时 FFT 频谱数据
窗口与系统 →浮窗、Now Playing、进程、图标
发布与分发 →插件源、市场、故障管理
内部 API 参考 →不暴露给插件的内部 API(FFmpeg 引擎等)