更新记录

1.0.0(2025-11-10) 下载此版本

音频文件播放后直接缓存到本地,减少请求网络资源次数,提高应用性能,支持并发、队列播放,提供相应的对外接口


平台兼容性

uni-app(4.86)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
- -
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 快应用-华为 快应用-联盟
- - - - - - - - - - -

其他

多语言 暗黑模式 宽屏模式
× ×

audio-cache (uni_modules)

概述

Uni-app音频缓存插件,具备:

  • H5 IndexedDB 持久化
  • LRU缓存元数据
  • 多个并发音频实例 + 播放队列
  • 事件:播放、暂停、停止、结束、错误、已缓存、缓存失败、下载开始、下载错误、时间更新
  • 支持Vue2和Vue3的Vue插件(this.$audioCache / app.config.globalProperties.$audioCache)
  • 包含TypeScript声明

安装使用

  1. uni_modules/uni-audio-cache复制到项目的uni_modules文件夹中。
  2. 注册插件:

    • Vue3:app.use(require('@/uni_modules/uni-audio-cache').default, options)
    • Vue2:Vue.use(require('@/uni_modules/uni-audio-cache').default, options)
    // 绑定事件
    this.$audioCache.on('play', ()=>{
    
    });
    this.$audioCache.on('started', ()=>{
    
    });
    this.$audioCache.on('timeupdate', ()=>{
    
    });
    this.$audioCache.on('ended', ()=>{
    
    });
    
    // 立即播放
    const id = await this.$audioCache.play('https://example.com/audio.mp3', { queue: false });
    
    // 加入队列播放
    const id = await this.$audioCache.addToQueue(this.url, { queue: true });
    
    // 暂停
    this.$audioCache.pause();
    // 停止
    this.$audioCache.stop();
    
    // 清除缓存
    await this.$audioCache.clear();

    API概述:

    • init(opts)// 初始化
    • play(url, opts) -> Promise opts:{ id?: string, loop?: boolean, startTime?: number, queue?: boolean, priority?: number }
    • addToQueue(url, opts) -> Promise
    • playNext()
    • pause(id?)
    • stop(id?)
    • clearQueue()
    • clear(url?) // 清除缓存条目或全部清除
    • meta(),idbEntries()
    • getPlayer(id), getAllPlayers(), getQueue() //获取播放实例
    • setMaxConcurrent(n)
    • on/off/once(event, handler)

更多:

  • H5使用IndexedDB来持久化音频Blob(如果可用)(options.useIndexedDB)。
  • 默认并发数为1。增加maxConcurrent以允许多个玩家同时进行游戏。

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议