赞
踩
一、准备工作
二、设计页面
- {
- "pages": [
- "pages/index/index"
- ],
- "window": {
- "backgroundTextStyle": "light",
- "navigationBarBackgroundColor": "#fff",
- "navigationBarTitleText": "Music Player",
- "navigationBarTextStyle": "black"
- }
- }
- <view class="container">
- <image src="{{music.cover}}" class="cover"></image>
- <view class="song-name">{{music.name}}</view>
- <view class="controls">
- <button bindtap="play" class="play-btn">{{playing ? '暂停' : '播放'}}</button>
- <button bindtap="prev" class="prev-btn">上一首</button>
- <button bindtap="next" class="next-btn">下一首</button>
- </view>
- </view>
- .container {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- height: 100vh;
- }
-
- .cover {
- width: 200rpx;
- height: 200rpx;
- border-radius: 50%;
- }
-
- .song-name {
- margin-top: 20rpx;
- font-size: 20rpx;
- }
-
- .controls {
- margin-top: 40rpx;
- }
-
- .play-btn,
- .prev-btn,
- .next-btn {
- width: 200rpx;
- height: 60rpx;
- background-color: #ff5000;
- border-radius: 30rpx;
- color: #fff;
- font-size: 24rpx;
- line-height: 60rpx;
- text-align: center;
- margin-bottom: 10rpx;
- }
- Page({
- data: {
- music: {
- cover: '/images/cover.jpg',
- name: '歌曲名称'
- },
- playing: false
- },
- play: function() {
- this.setData({
- playing: !this.data.playing
- });
- },
- prev: function() {
- // 上一首操作
- },
- next: function() {
- // 下一首操作
- }
- });
三、添加音乐播放功能
{ "pages": [ "pages/index/index" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "Music Player", "navigationBarTextStyle": "black" }, "permission": { "scope.userLocation": { "desc": "获取用户地理位置信息" }, "scope.record": { "desc": "录音功能" }, "scope.writePhotosAlbum": { "desc": "保存到相册" }, "scope.camera": { "desc": "拍照功能" } } }
- Page({
- data: {
- music: {
- cover: '/images/cover.jpg',
- name: '歌曲名称',
- src: '/music/song.mp3' // 音乐文件的路径
- },
- playing: false
- },
- audioContext: null,
- onLoad: function() {
- this.audioContext = wx.createInnerAudioContext();
- this.audioContext.src = this.data.music.src;
- },
- play: function() {
- if (this.data.playing) {
- this.audioContext.pause();
- } else {
- this.audioContext.play();
- }
- this.setData({
- playing: !this.data.playing
- });
- },
- prev: function() {
- // 上一首操作
- },
- next: function() {
- // 下一首操作
- }
- });
<button bindtap="play" class="play-btn">{{playing ? '暂停' : '播放'}}</button>
- const path = require('path');
- const fs = require('fs');
-
- Page({
- // ...
- onLoad: function() {
- const filePath = path.join(wx.env.USER_DATA_PATH, 'song.mp3');
- this.downloadFile(this.data.music.src, filePath, () => {
- this.audioContext.src = filePath;
- });
- },
- downloadFile: function(url, filePath, success) {
- wx.downloadFile({
- url: url,
- header: {},
- filePath: filePath,
- success: function(res) {
- console.log(res);
- success();
- },
- fail: function(res) {
- console.log(res);
- },
- complete: function(res) {
- console.log(res);
- }
- });
- },
- // ...
- });
四、添加音乐切换功能
- Page({
- // ...
- prev: function() {
- // 获取音乐列表
- const musicList = [];
-
- // 找到当前音乐的索引
- let currentIndex = 0;
- for (let i = 0; i < musicList.length; i++) {
- if (musicList[i].src === this.data.music.src) {
- currentIndex = i;
- break;
- }
- }
-
- // 计算上一首音乐的索引
- let prevIndex = currentIndex - 1;
- if (prevIndex < 0) {
- prevIndex = musicList.length - 1;
- }
-
- // 切换到上一首音乐
- const prevMusic = musicList[prevIndex];
- this.setData({
- music: prevMusic,
- playing: true
- });
-
- const filePath = path.join(wx.env.USER_DATA_PATH, 'song.mp3');
- this.downloadFile(prevMusic.src, filePath, () => {
- this.audioContext.src = filePath;
- this.audioContext.play();
- });
- },
- next: function() {
- // 获取音乐列表
- const musicList = [];
-
- // 找到当前音乐的索引
- let currentIndex = 0;
- for (let i = 0; i < musicList.length; i++) {
- if (musicList[i].src === this.data.music.src) {
- currentIndex = i;
- break;
- }
- }
-
- // 计算下一首音乐的索引
- let nextIndex = currentIndex + 1;
- if (nextIndex >= musicList.length) {
- nextIndex = 0;
- }
-
- // 切换到下一首音乐
- const nextMusic = musicList[nextIndex];
- this.setData({
- music: nextMusic,
- playing: true
- });
-
- const filePath = path.join(wx.env.USER_DATA_PATH, 'song.mp3');
- this.downloadFile(nextMusic.src, filePath, () => {
- this.audioContext.src = filePath;
- this.audioContext.play();
- });
- },
- // ...
- });
五、完善播放界面
- <view class="progress">
- <progress bindchange="seek" show-info="{{true}}" duration="{{audioContext.duration}}" value="{{audioContext.currentTime}}"></progress>
- </view>
- <view class="time">
- {{formatTime(audioContext.currentTime)}} / {{formatTime(audioContext.duration)}}
- </view>
- Page({
- data: {
- music: {
- cover: '/images/cover.jpg',
- name: '歌曲名称',
- src: '/music/song.mp3'
- },
- playing:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。