微信小程序之音频播放-wx.createInnerAudioContext()注意点

咱们先看看官方示例

地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/createInnerAudioContext.html

 

创建innerAudioContext 对象。

autoplay自动播放开启,

src音频路径

onPlay()以及onError()播放以及错误回调。

以上理解的不难懂。

 

那么问题来。大部分人都会直接运用在自己需要的事件中。比如:

Page外:

 

const innerAudioContext = wx.createInnerAudioContext()

 

Page内:

 

audioPlay: function() {

        innerAudioContext.src = 'xxx';

        innerAudioContext.play();

 

 

        innerAudioContext.onPlay(() => {

            console.log('录音播放中');

        })

 

        innerAudioContext.onStop(() => {

            console.log('录音播放停止');

        })

 

        innerAudioContext.onEnded(() => {

            console.log('录音播放结束');

        })

 

  }

})

直接触发audioPlay方法 简单粗暴是不

结果发现。innerAudioContext.onPlay(),innerAudioContext.onStop(),innerAudioContext.onEnded(),播放停止多少次,就会叠加回调。

意外不意外。惊喜不惊喜。

其实不是官方误导我们,而是我们理解错了 

回调事件,是每一次触发都会注册一次。

也就是这个事件触发过一次。这个回调就已经一直存在了。所以不需要一直去创建回调事件。

那么应该怎么做呢~

 

只需要把所有的回调写在onLoad中即可。页面加载即注册监听事件,小程序中即使定义了全局变量,事件绑定也不会覆盖,只会加注册多一个线程监听

 

控制播放在其他事件进行即可。回调只需要在onLoad中注册就可以了

<think>根据提供的引用内容,用户查询的重是:在微信小程序中使用 `wx.createInnerAudioContext()` 获取音频实例总时长的方法。引用[1]和引用[3]中提到了相关代码,其中通过 `innerAudioContext.duration` 属性获取音频总时长。 具体步骤如下: 1. 创建音频实例:`this.innerAudioContext = wx.createInnerAudioContext()` 2. 设置音频源:`this.innerAudioContext.src = '音频链接'` 3. 播放音频(或确保音频已加载):`this.innerAudioContext.play()` 4. 监听 `onTimeUpdate` 事件,在回调中通过 `this.innerAudioContext.duration` 获取总时长 注意:`duration` 属性在音频加载完成后才会有效。根据微信小程序官方文档,`duration` 在音频未加载时可能为0,因此需要在合适的时机获取(如 `onTimeUpdate` 或 `onCanplay` 事件中)。 示例代码: ```javascript // 创建音频实例 this.innerAudioContext = wx.createInnerAudioContext(); this.innerAudioContext.src = 'http://example.com/audio.mp3'; // 监听音频可以播放事件(此时可能已获取到时长) this.innerAudioContext.onCanplay(() => { console.log('音频总时长:', this.innerAudioContext.duration); }); // 或者监听时间更新事件(会频繁触发) this.innerAudioContext.onTimeUpdate(() => { console.log('当前进度:', this.innerAudioContext.currentTime, '总时长:', this.innerAudioContext.duration); }); // 开始播放以加载音频(如果需要立即加载而不播放,可以使用 play 后立即 pause) this.innerAudioContext.play(); ``` 重要:在真实场景中,音频总时长可能不会立即获得,因此需要事件监听。如果用户不需要播放,只想预加载获取时长,可以在 `onCanplay` 中获取,然后暂停播放。 §§相关问题§§ 1. 如何在小程序音频播放器中实现进度条功能? 2. `wx.createInnerAudioContext()` 和 `<audio>` 组件有什么区别? 3. 微信小程序中如何处理音频播放的多实例控制? </think>### 微信小程序获取音频时长的方法 在微信小程序中使用 `wx.createInnerAudioContext()` 获取音频总时长,需通过以下步骤实现: 1. **创建音频实例** ```javascript this.innerAudioContext = wx.createInnerAudioContext(); ``` 2. **设置音频源** 引用[1][^1]和引用[3][^3]中的示范: ```javascript this.innerAudioContext.src = 'http://.../audio.mp3'; // 替换为实际音频URL ``` 3. **监听时长获取时机** **关键**:音频时长需在元数据加载完成后才能获取,推荐两种监听方式: - **方式1:通过 `onCanplay` 事件**(音频播放时触发) ```javascript this.innerAudioContext.onCanplay(() => { const duration = this.innerAudioContext.duration; console.log('音频总时长:', duration); }); ``` - **方式2:通过 `onTimeUpdate` 事件**(播放进度更新时触发,需先播放) ```javascript // 引用[1]中的示例[^1] this.innerAudioContext.play(); // 必须先启动播放 this.innerAudioContext.onTimeUpdate(() => { const duration = this.innerAudioContext.duration; console.log('音频总时长:', duration); }); ``` 4. **完整示例代码** ```javascript Page({ data: { audioDuration: 0 }, onLoad() { this.audio = wx.createInnerAudioContext(); this.audio.src = 'https://example.com/audio.mp3'; // 监听元数据加载 this.audio.onCanplay(() => { this.setData({ audioDuration: this.audio.duration }); }); }, // 手动触发获取(需要用户交互) getDuration() { this.audio.play(); // 开始播放以激活音频 setTimeout(() => { this.audio.pause(); // 立即暂停 console.log('时长:', this.audio.duration); }, 500); } }) ``` ### 注意事项 1. **`duration` 属性初始值为 `NaN`** 音频加载前直接访问 `innerAudioContext.duration` 会得到 `NaN`,必须等待 `onCanplay` 或 `onTimeUpdate` 事件触发。 2. **用户交互限制** 微信小程序要求音频播放必须由用户触发(如按钮击),直接调用 `play()` 可能无效。可在按钮事件中触发播放: ```xml <button bindtap="getDuration">获取时长</button> ``` 3. **自动播放策略** 小程序自动播放需满足: - iOS:始终需要用户手势触发 - Android:首次播放需用户触发,后续可通过代码控制 ---
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值