wx.playBackgroundAudio(OBJECT)
能播放就是识别不出duration
得播完了再播有时候才有
目前我写的一个,感觉还是有不少的问题,就是未播放的时候无法获取音频时长,只能默认为00:00,点击播放的时候才会获取时长。倒计时显示,在开发工具上暂停和播放还可以,但是用真机就不行了,卡顿相当严重,仅供参考。
页面
<view>
<image src="/image/{{isPlay?‘stop’:‘play’}}.png" class=“audio_image” bindtap=“play”></image>
</view>
js控制
Page({
data: { isPlay: false },
onLoad: function (options) {
},
onReady: function (e) {
},
play: function () {
var isPlay = this.data.isPlay;
if (isPlay) {
wx.pauseBackgroundAudio();
this.setData({ isPlay: false });
} else {
wx.playBackgroundAudio({
dataUrl: “”,
title: “”,
coverImgUrl: “”
});
this.setData({ isPlay: true });
}
countdown(this);
}
})
function countdown(that) {
wx.getBackgroundAudioPlayerState({
success: function (res) {
if (res.status == 1) {
var time = setTimeout(function () {
that.setData({ time: dateformat(res.duration - res.currentPosition) });
countdown(that);
}, 500);
}
},
fail: function () {
that.setData({ time: “00:00” });
if (that.data.isPlay) {
var time = setTimeout(function () {
countdown(that);
}, 1000);
}
}
});
}
function dateformat(second) {
var dateStr = “”;
var min = Math.floor(second / 60);
var sec = (second - min * 60);
dateStr = min + “:” + (sec < 10 ? “0” + sec : sec);
return dateStr;
}