SepYoveL 2023-10-16 11:41 采纳率: 40%
浏览 21

微信小程序音乐小程序


// pages/index/index.js
Page({

/**
* 页面的初始数据
*/
data: {
current: 0,
// 播放列表数据
playlist: [{
id: 1,
title: '钢琴协奏曲',
singer: '肖邦',
src: 'http://localhost:3000/1.mp3',
coverImgUrl: '/images/cover.jpg'
}, {
id: 2,
title: '奏鸣曲',
singer: '莫扎特',
src: 'http://localhost:3000/2.mp3',
coverImgUrl: '/images/cover.jpg'
}, {
id: 3,
title: '欢乐颂',
singer: '贝多芬',
src: 'http://localhost:3000/1.mp3',
coverImgUrl: '/images/cover.jpg'
}, {
id: 4,
title: '爱之梦',
singer: '李斯特',
src: 'http://localhost:3000/2.mp3',
coverImgUrl: '/images/cover.jpg'
}],
state: 'paused',
playIndex: 0,
play: {
currentTime: '00:00',
duration: '00:00',
percent: 0,
title: '',
singer: '',
coverImgUrl: '/images/cover.jpg',
}
},

// 页面切换
changeItem: function(e) {
this.setData({
current: e.target.dataset.item,
})
},


// 实现播放器播放功能
audioCtx: null,
onReady: function() {
this.audioCtx = wx.createInnerAudioContext()
// 默认选择第1曲
this.setMusic(0)
var that = this
// 播放进度检测
this.audioCtx.onError(function() {
console.log('播放失败:' + that.audioCtx.src)
})
// 播放完成自动换下一曲
this.audioCtx.onEnded(function() {
that.next()
})
// 自动更新播放进度
this.audioCtx.onPlay(function() {})
this.audioCtx.onTimeUpdate(function() {
that.setData({
'play.duration': formatTime(that.audioCtx.duration),
'play.currentTime': formatTime(that.audioCtx.currentTime),
'play.percent': that.audioCtx.currentTime / that.audioCtx.duration * 100
})
})
// 格式化时间
function formatTime(time) {
var minute = Math.floor(time / 60) % 60;
var second = Math.floor(time) % 60
return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
}
},
// 音乐播放
setMusic: function(index) {
var music = this.data.playlist[index]
this.audioCtx.src = music.src
this.setData({
playIndex: index,
'play.title': music.title,
'play.singer': music.singer,
'play.coverImgUrl': music.coverImgUrl,
'play.currentTime': '00:00',
'play.duration': '00:00',
'play.percent': 0
})
},

// 播放按钮
play: function() {
this.audioCtx.play()
this.setData({
state: 'running'
})
},

// 暂停按钮
pause: function() {
this.audioCtx.pause()
this.setData({
state: 'paused'
})
},

// 下一曲按钮
next: function() {
var index = this.data.playIndex >= this.data.playlist.length - 1 ? 0 : this.data.playIndex + 1
this.setMusic(index)
if (this.data.state === 'running') {
this.play()
}
},

// 滚动条调节歌曲进度
sliderChange: function(e) {
var second = e.detail.value * this.audioCtx.duration / 100
this.audioCtx.seek(second)
},

// 播放列表换曲功能
change: function(e) {
this.setMusic(e.currentTarget.dataset.index)
this.play()
}
})

在课堂音乐小程序的基础上,继续完善音乐推荐页面推荐歌曲功能。具体要实现的功能:用户点击某一首推荐音乐,该音乐添加到播放列表里,并开始播放。请详细描述实现思路,对关键代码进行解释,附上可以体现该功能的截图,并提交完整代码压缩包。

  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-10-16 20:35
    关注

    【以下回答由 GPT 生成】

    对于以上代码,有几个问题需要进行优化:

    1. 播放列表的音乐地址(src)都是相同的,无法实现播放不同的音乐。
    2. 播放器没有暂停、播放、停止、上一曲、下一曲等功能的实现。
    3. 页面切换没有对应的处理函数。

    下面是对以上问题的具体优化方案:

    1. 修改播放列表的音乐地址(src)为不同的地址,确保可以播放不同的音乐。
    playlist: [{
      id: 1,
      title: '钢琴协奏曲',
      singer: '肖邦',
      src: 'http://localhost:3000/1.mp3',
      coverImgUrl: '/images/cover.jpg'
    }, {
      id: 2,
      title: '奏鸣曲',
      singer: '莫扎特',
      src: 'http://localhost:3000/2.mp3',
      coverImgUrl: '/images/cover.jpg'
    }, {
      id: 3,
      title: '欢乐颂',
      singer: '贝多芬',
      src: 'http://localhost:3000/3.mp3',
      coverImgUrl: '/images/cover.jpg'
    }, {
      id: 4,
      title: '爱之梦',
      singer: '李斯特',
      src: 'http://localhost:3000/4.mp3',
      coverImgUrl: '/images/cover.jpg'
    }],
    
    1. 实现播放器的暂停、播放、停止、上一曲、下一曲等功能。
    // 实现播放器的暂停功能
    pause: function() {
      this.audioCtx.pause()
      this.setData({
        state: 'paused'
      })
    },
    
    // 实现播放器的播放功能
    play: function() {
      this.audioCtx.play()
      this.setData({
        state: 'running'
      })
    },
    
    // 实现播放器的停止功能
    stop: function() {
      this.audioCtx.stop()
      this.setData({
        state: 'stopped',
        'play.currentTime': '00:00',
        'play.percent': 0
      })
    },
    
    // 实现播放器的上一曲功能
    prev: function() {
      let playIndex = this.data.playIndex
      if (playIndex === 0) {
        playIndex = this.data.playlist.length - 1
      } else {
        playIndex--
      }
      this.setMusic(playIndex)
    },
    
    // 实现播放器的下一曲功能
    next: function() {
      let playIndex = this.data.playIndex
      if (playIndex === this.data.playlist.length - 1) {
        playIndex = 0
      } else {
        playIndex++
      }
      this.setMusic(playIndex)
    },
    
    1. 处理页面切换的函数changeItem。
    changeItem: function(e) {
      let item = e.target.dataset.item
      if (item === 'play') {
        this.play()
      } else if (item === 'pause') {
        this.pause()
      } else if (item === 'stop') {
        this.stop()
      } else if (item === 'prev') {
        this.prev()
      } else if (item === 'next') {
        this.next()
      }
    },
    

    最后,完整的优化代码如下:

    // pages/index/index.js
    Page({
      data: {
        current: 0,
        playlist: [{
          id: 1,
          title: '钢琴协奏曲',
          singer: '肖邦',
          src: 'http://localhost:3000/1.mp3',
          coverImgUrl: '/images/cover.jpg'
        }, {
          id: 2,
          title: '奏鸣曲',
          singer: '莫扎特',
          src: 'http://localhost:3000/2.mp3',
          coverImgUrl: '/images/cover.jpg'
        }, {
          id: 3,
          title: '欢乐颂',
          singer: '贝多芬',
          src: 'http://localhost:3000/3.mp3',
          coverImgUrl: '/images/cover.jpg'
        }, {
          id: 4,
          title: '爱之梦',
          singer: '李斯特',
          src: 'http://localhost:3000/4.mp3',
          coverImgUrl: '/images/cover.jpg'
        }],
        state: 'paused',
        playIndex: 0,
        play: {
          currentTime: '00:00',
          duration: '00:00',
          percent: 0,
          title: '',
          singer: '',
          coverImgUrl: '/images/cover.jpg',
        },
        audioCtx: null
      },
    
      changeItem: function(e) {
        let item = e.target.dataset.item
        if (item === 'play') {
          this.play()
        } else if (item === 'pause') {
          this.pause()
        } else if (item === 'stop') {
          this.stop()
        } else if (item === 'prev') {
          this.prev()
        } else if (item === 'next') {
          this.next()
        }
      },
    
      onReady: function() {
        this.setData({
          audioCtx: wx.createInnerAudioContext()
        })
        this.setMusic(0)
        let that = this
        this.data.audioCtx.onError(function() {
          console.log('播放失败:' + that.data.audioCtx.src)
        })
        this.data.audioCtx.onEnded(function() {
          that.next()
        })
        this.data.audioCtx.onTimeUpdate(function() {
          that.setData({
            'play.duration': formatTime(that.data.audioCtx.duration),
            'play.currentTime': formatTime(that.data.audioCtx.currentTime),
            'play.percent': that.data.audioCtx.currentTime / that.data.audioCtx.duration * 100
          })
        })
        function formatTime(time) {
          let minute = Math.floor(time / 60) % 60
          let second = Math.floor(time) % 60
          return minute + ':' + second
        }
      },
    
      setMusic: function(playIndex) {
        let music = this.data.playlist[playIndex]
        this.data.audioCtx.src = music.src
        this.data.audioCtx.title = music.title
        this.data.audioCtx.singer = music.singer
        this.data.audioCtx.coverImgUrl = music.coverImgUrl
        this.setData({
          play: {
            currentTime: '00:00',
            duration: '00:00',
            percent: 0,
            title: music.title,
            singer: music.singer,
            coverImgUrl: music.coverImgUrl
          },
          playIndex: playIndex
        })
      },
    
      pause: function() {
        this.data.audioCtx.pause()
        this.setData({
          state: 'paused'
        })
      },
    
      play: function() {
        this.data.audioCtx.play()
        this.setData({
          state: 'running'
        })
      },
    
      stop: function() {
        this.data.audioCtx.stop()
        this.setData({
          state: 'stopped',
          'play.currentTime': '00:00',
          'play.percent': 0
        })
      },
    
      prev: function() {
        let playIndex = this.data.playIndex
        if (playIndex === 0) {
          playIndex = this.data.playlist.length - 1
        } else {
          playIndex--
        }
        this.setMusic(playIndex)
      },
    
      next: function() {
        let playIndex = this.data.playIndex
        if (playIndex === this.data.playlist.length - 1) {
          playIndex = 0
        } else {
          playIndex++
        }
        this.setMusic(playIndex)
      }
    })
    

    请注意,在以上代码中,我使用了该小程序的内部音频控制对象wx.createInnerAudioContext()来创建一个用于播放音频的实例对象。它提供了一系列的方法以及事件供开发者使用。



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 10月16日

悬赏问题

  • ¥15 Kettle 抽取Excel转换问题
  • ¥15 ubuntu 20.04 网卡启用后,只有ipv6 没有 ipv4 无法上网
  • ¥15 QT任务管理器无法正确获取展开,有悬赏15元速求,如何解决?(相关搜索:标识符|结构体)
  • ¥15 使用delphi 10.3+intraweb 生成的页面怎么实现自动滚屏
  • ¥20 思科:Router c3600 MN-4E插槽
  • ¥15 16进制修改视频的元数据
  • ¥15 HTML中css的位置信息居然会导致元素大小发生变化
  • ¥15 岛津txt格式文件转nirs格式
  • ¥15 石墨烯磁表面等离子体
  • ¥15 angular 项目无法启动