zoukankan      html  css  js  c++  java
  • 微信小程序播放背景音乐

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

  • 相关阅读:
    apache伪静态设置
    ZeroClipboard.js兼容各种浏览器复制到剪切板上
    table 如何给tr border颜色
    JSON用法之将PHP数组转JS数组,JS如何接收PHP数组
    jquery操作select(增加,删除,清空)
    JS生成随机的由字母数字组合的字符串
    Redis连接(二)
    Redis集群(一)
    wap启用宏
    windows 10激活
  • 原文地址:https://www.cnblogs.com/haonanZhang/p/9265460.html
Copyright © 2011-2022 走看看