Skip to content

微信小程序 wx.getBackgroundAudioManager API中的一些坑 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wusb opened this issue Mar 18, 2018 · 0 comments
Open

微信小程序 wx.getBackgroundAudioManager API中的一些坑 #6

wusb opened this issue Mar 18, 2018 · 0 comments

Comments

@wusb
Copy link
Owner

wusb commented Mar 18, 2018

(此总结只保证对基础库1.9.91有效)

onCanplay

onCanplay在真机及开发工具上均无法触发,因此无法在此生命周期执行相关操作

onPlay

onPlay开始播放音频,我们通常会考虑在这个生命周期内获取音频的长度duration,但是回调函数内并不能实时获取,感觉违背了常识。可以采用setTimeout设定一个延时来获取,但也做不到实时,建议在 onTimeUpdate回调内获取我们需要的duration。

onPause

在此生命周期,我们希望能获取音频的播放状态paused,来做一些逻辑上的处理,但并不能准备获取正确的值true,此处需要手动设置。比如:

const audio = wx.getBackgroundAudioManager();
audio.onPause(() => {
    //此时audio.paused,按道理来说是true,并不一定是true
    self.setData({
        pause: audio.paused
    })
    //所以需要显式的去设置
    self.setData({
        pause: true
    })
})

seek

文档写的参数是postion,如果是首次接触很容易误导,其实参数就是一个Number,比如:

const audio = wx.getBackgroundAudioManager();
audio.seek(500);

title

如果不设置的话,在iOS设备上会导致音频无法播放,Android上影响不大。

全局对象

const audio = wx.getBackgroundAudioManager();

audio 获取是一个全局的音频对象,如果一个页面里面定义多个audio,它们的生命周期是共享的。比如说,我们在同一个page的两个function内均定义了audio,并且,都使用了onTimeUpdate这个生命周期,那么这两个onTimeUpdate是共享的,并不会同时存在,只会以最近一次调用的audio为准。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant