html背景音乐微信播放器,微信小程序播放背景音乐(示例代码)

小程序实现和h5一样的音乐图标一直旋转。

一、.js中封装旋转动画方法

添加animation属性

data:{

animation:‘‘"}

改变animation的值(官网提供角度范围是-180~180,但是我发现角度越大会一直旋转)

onShow: function() {

console.log(‘index---------onShow()‘)this.animation =wx.createAnimation({

duration:1400,

timingFunction:‘linear‘, //"linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"

delay: 0,

transformOrigin:‘50% 50% 0‘,

success: function(res) {

console.log("res")

}

})

},

rotateAni: function (n) {

console.log("rotate=="+n)this.animation.rotate(180*(n)).step()this.setData({

animation:this.animation.export()

})

},

二、在.wxml中需要的控件上添加animation属性

三、连续动画需要添加定时器

var n = 0,

that= this;this.interval =setInterval(function () {

n++;

that.rotateAni(n);

},1400);

onLoad 或者 show 的时候执行或者看业务需求。

微信达到播放音乐的效果

一、方法一。

点击的时候播放一段 语音(也可以是音乐),这种方法不适合做背景音乐,类似h5的那样背景音乐。

点击音乐图标的时候

onmusicTap: function (event) {if (this.data.isPlayingMusic) {

wx.pauseBackgroundAudio();this.setData({

isPlayingMusic:false});

}else{

wx.playBackgroundAudio({

dataUrl:‘https://www.zhitaochan.cn/Opening.mp3‘});this.setData({

isPlayingMusic:true});

}

},

判断当前是否在在播放这段音乐,没有则播放,播放中就不执行播放。

在onLoad的时候执行

wx.playBackgroundAudio({

dataUrl:‘https://www.zhitaochan.cn/Opening.mp3‘});

方法二、H5一样的背景音乐,一直播放可循环

onLoad的时候

const innerAudioContext =wx.createInnerAudioContext();

innerAudioContext.autoplay= true;

innerAudioContext.src= ‘https://www.zhitaochan.cn/Opening.mp3‘;

innerAudioContext.loop= true;

innerAudioContext.play();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值