微信小程序【三】超Q旋转音乐播放器

效果简述:登录自动播放背景音乐,点击图标控制音乐的播放和暂停,再次点击即暂停图标旋转的效果。

在这里插入图片描述

步骤一:修改 app.json

确保 app.json 中设置了页面路径并且导航栏样式为 custom。

"window": {
    "navigationStyle": "custom"
  }

步骤二:.wxml文件

在对应的.wxml中,添加播放图标以index.wxml为例,src中放入你的图标图片:

<view class="container">
  <image class="music-icon" src="../images/music_icon.png" bindtap="togglePlay" style="transform: {{currentTransform}};"></image>
</view>

步骤三:.wxss文件

在对应页面的.wxss文件中定义图标旋转的样式,以index.wxss为例:

.container {
  position: absolute;
  top: 20rpx;
  right: 20rpx;
}

.music-icon {
  width: 50rpx;
  height: 50rpx;
}

.playing {
  animation: rotate 2s linear infinite;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

步骤四:.js文件

在页面逻辑 index.js 中实现音乐播放和控制逻辑:

// index.js
Page({
  data: {
    isPlaying: true,
    animationClass: 'playing',
    currentTransform: 'rotate(0deg)',
    currentRotation: 0
  },

  onLoad: function() {
    this.backgroundAudioManager = wx.getBackgroundAudioManager();
    this.backgroundAudioManager.title = '背景音乐';
    this.backgroundAudioManager.src = 'http://xxxx/jar.mp3';
    this.backgroundAudioManager.play();
    this.startRotation();
  },

  startRotation: function() {
    if (this.rotationInterval) {
      clearInterval(this.rotationInterval);
    }

    this.rotationInterval = setInterval(() => {
      if (this.data.isPlaying) {
        this.setData({
          currentRotation: this.data.currentRotation + 1,
          currentTransform: `rotate(${this.data.currentRotation}deg)`
        });
      }
    }, 20); // Adjust the interval as needed
  },

  togglePlay: function() {
    if (this.data.isPlaying) {
      this.backgroundAudioManager.pause();
      clearInterval(this.rotationInterval);
      this.setData({
        isPlaying: false
      });
    } else {
      this.backgroundAudioManager.play();
      this.setData({
        isPlaying: true
      });
      this.startRotation();
    }
  }
});


页面逻辑

your-project/
|-- images/
|   |-- music_icon.png
|-- pages/
|   |-- index/
|       |-- index.js
|       |-- index.wxml
|       |-- index.wxss
|-- app.json

通过以上步骤,我们的音乐就能进行播放了,具体可扩展。
例如:循环+播放多首歌

五、扩展功能

5.1、.js文件

Page({
  data: {
    isPlaying: true,
    animationClass: 'playing',
    currentTransform: 'rotate(0deg)',
    currentRotation: 0,
    playlist: [
      'http://xxx/mxd1.mp3',
      'http://xxx/mxd2.mp3',
      'http://xxx/mxd3.mp3'
    ],
    currentTrackIndex: 0
  },

  onLoad: function() {
    this.backgroundAudioManager = wx.getBackgroundAudioManager();
    this.playCurrentTrack();

    this.backgroundAudioManager.onEnded(() => {
      this.nextTrack();
    });

    this.startRotation();
  },

  playCurrentTrack: function() {
    const currentTrack = this.data.playlist[this.data.currentTrackIndex];
    this.backgroundAudioManager.title = '背景音乐';
    this.backgroundAudioManager.src = currentTrack;
    this.backgroundAudioManager.play();
  },

  nextTrack: function() {
    let nextIndex = this.data.currentTrackIndex + 1;
    if (nextIndex >= this.data.playlist.length) {
      nextIndex = 0; // Loop back to the first track
    }
    this.setData({
      currentTrackIndex: nextIndex
    });
    this.playCurrentTrack();
  },

  startRotation: function() {
    if (this.rotationInterval) {
      clearInterval(this.rotationInterval);
    }

    this.rotationInterval = setInterval(() => {
      if (this.data.isPlaying) {
        this.setData({
          currentRotation: this.data.currentRotation + 1,
          currentTransform: `rotate(${this.data.currentRotation}deg)`
        });
      }
    }, 20); // Adjust the interval as needed
  },

  togglePlay: function() {
    if (this.data.isPlaying) {
      this.backgroundAudioManager.pause();
      clearInterval(this.rotationInterval);
      this.setData({
        isPlaying: false
      });
    } else {
      this.backgroundAudioManager.play();
      this.setData({
        isPlaying: true
      });
      this.startRotation();
    }
  }
});

5.2、.wxml文件

<!--index.wxml-->
<navigation-bar title="标题写在这里" back="{{false}}" color="black" background="#FFF"></navigation-bar>

<!--轮播图区域-->
<view class="swiper">
  <swiper autoplay interval="3000" indicator-dots="true" indicator-color="white" indicator-active-color="yellow">
    <swiper-item>
      <image src="http://xxx/1.png"></image>
    </swiper-item>
    <swiper-item>
      <image src="http://xxx/2.png"></image>
    </swiper-item>
   
  </swiper>

  <view class="container">
    <image class="music-icon" src="../images/music_icon.png" bindtap="togglePlay" style="transform: {{currentTransform}};"></image>
  </view>



  <view class="marquee">
    <span style="color: rgb(7, 7, 7);">这是走马灯效果</span>
  </view>
</view>

5.3、.wxss文件

/**index.wxss**/
page {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

swiper {
  width: 100%;
  height: 250px;
}

swiper image {
  width: 100%;
  height: 100%;
}

.container {
  position: absolute;
  top: 40rpx;
  right: 40rpx;
}

.music-icon {
  width: 80rpx;
  height: 80rpx;
}

.playing {
  animation: rotate 2s linear infinite;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 跑马灯效果 */
.marquee {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
}

.marquee span {
  font-size: 25px;
  font-weight: bold;
  display: inline-block;
  padding-left: 100%;
  animation: marquee 18s linear infinite;
}

@keyframes marquee {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(-100%, 0);
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sinysama

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值