• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

微信小程序播放背景音乐

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

小程序实现和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属性

<view class="show-iconView">
      <image src="{{src}}" class="show-iconImage" animation="{{animation}}" mode="scaleToFill"></image>
  </view>

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

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();

具体属性:https://developers.weixin.qq.com/miniprogram/dev/api/createInnerAudioContext.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
小程序音乐播放器发布时间:2022-07-18
下一篇:
微信小程序音频 ,背景音乐发布时间:2022-07-18
热门推荐
    热门话题
    阅读排行榜

    扫描微信二维码

    查看手机版网站

    随时了解更新最新资讯

    139-2527-9053

    在线客服(服务时间 9:00~18:00)

    在线QQ客服
    地址:深圳市南山区西丽大学城创智工业园
    电邮:jeky_zhao#qq.com
    移动电话:139-2527-9053

    Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap