微信小程序 第三章 “音乐”小程序项目

这篇具有很好参考价值的文章主要介绍了微信小程序 第三章 “音乐”小程序项目。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


已经更新,可以在手机上进行调试
  1. 图片利用图床生成在线链接
  2. 网易云音乐MP3地址为http://music.163.com/song/media/outer/url?id= .mp3
    微信小程序 第三章 “音乐”小程序项目

学习目标

  1. 掌握swiper组件的使用
  2. 掌握scroll-view组件的使用
  3. 掌握image组件的使用
  4. 掌握slider组件的使用
  5. 掌握音频API的使用

具体使用请看微信开发文档

开发前准备

项目展示

微信小程序 第三章 “音乐”小程序项目微信小程序 第三章 “音乐”小程序项目微信小程序 第三章 “音乐”小程序项目

项目分析

页面结构

  • 页面由上、中、下三个区域组成(tabcontentplayer
  • content通过左右滑动可以实现标签页的转换
  • 音乐推荐向用户推荐热门音乐
  • 播放器显示当前播放进度,用户可以跳转进度
  • 播放列表显示当前播放的曲目列表,用户可以进行曲目切换
  • player三个按钮 打开播放列表 播放暂停 切换到下一曲

目录结构

微信小程序 第三章 “音乐”小程序项目微信小程序 第三章 “音乐”小程序项目

标签页切换–index页面

<!-- 顶部 -->
<view class="tab">
  <view class="tab-item {{tab==0?'active':''}}" bindtap="changeItem" data-item="0">音乐推荐</view>
  <view class="tab-item {{tab==1?'active':''}}" bindtap="changeItem" data-item="1">播放器</view>
  <view class="tab-item {{tab==2?'active':''}}" bindtap="changeItem" data-item="2">播放列表</view>
</view>
<!-- 内容(可滑动在三个页面之间进行转换) -->
<view class="content">
  <swiper current="{{item}}" bindchange="changeTab">
    <swiper-item><include src="info.wxml"></include></swiper-item>
    <swiper-item><include src="play.wxml"></include></swiper-item>
    <swiper-item><include src="playlist.wxml"></include></swiper-item>
  </swiper>
</view>
<!-- 底部 -->
<view class="player">
  <image class="player-cover" src="{{play.coverImgUrl}}"/>
  <view class="player-info">
    <view class="player-info-title">{{play.title}}</view>
    <view class="player-info-singer">{{play.singer}}</view>
  </view>
  <view class="player-controls">
    <!-- 切换到播放列表 -->
    <image src="../images/01.png" bindtap="changePage" data-page="2" />
    <!-- 播放 -->
    <image wx:if="{{state=='paused'}}" src="../images/02.png" bindtap="play" />
    <image wx:else src="../images/02stop.png" bindtap="pause" />
    <!-- 下一曲 -->
    <image src="../images/03.png" bindtap="next"/>
  </view>
</view>

音乐推荐

<scroll-view class="content-info" scroll-y>
  <!-- 轮播图 -->
  <swiper class="content-info-slide" indicator-color="rgba(255,255,255,0.5)" indicator-active-color="#fff" indicator-dots autoplay>
    <swiper-item><image src="../images/b1.jpg" /></swiper-item>
    <swiper-item><image src="../images/b2.jpg" /></swiper-item>
    <swiper-item><image src="../images/b3.jpg" /></swiper-item>
  </swiper>
  <!-- 功能按钮 -->
  <view class="content-info-portal">
    <view><image src="../images/04.png" /><text>私人FM</text></view>
    <view><image src="../images/05.png" /><text>每日歌曲推荐</text></view>
    <view><image src="../images/06.png" /><text>云音乐新歌榜</text></view>
  </view>
  <!-- 热门音乐 -->
  <view class="content-info-list">
    <view class="list-title">推荐歌曲</view>
    <view class="list-inner">
      <view wx:if="{{index<6}}" wx:for="{{playlist}}" wx:key="id" bindtap="change" data-index="{{index}}" class="list-item">
        <image src="{{item.coverImgUrl}}"></image>
        <view>{{item.title}}</view>
      </view>
  </view>
</scroll-view>

播放器页面

<view class="content-play">
  <!-- 显示音乐信息 -->
  <view class="content-play-info">
    <text>{{play.title}}</text>
    <view>——  {{play.singer}}  ——</view>
  </view>
  <!-- 显示专辑封面 -->
  <view class="content-play-cover">
    <image src="{{play.coverImgUrl}}" style="animation-play-state:{{state}}" />
  </view>
  <!-- 显示播放进度和时间 -->
  <view class="content-play-progress">
    <text>{{play.currentTime}}</text>
    <view><slider bindchange="sliderChange" activeColor="#d33a31" block-size="12" backgroundColor="#dadada" value="{{play.percent}}" /></view>
    <text>{{play.duration}}</text>
  </view>
</view>

播放列表

<scroll-view class="content-playlist" scroll-y>
  <view class="playlist-item" wx:for="{{playlist}}" wx:key="id" bindtap="change" data-index="{{index}}">
    <image class="playlist-cover" src="{{item.coverImgUrl}}" />
    <view class="playlist-info">
      <view class="playlist-info-title">{{item.title}}</view>
      <view class="playlist-info-singer">{{item.singer}}</view>
    </view>
    <view class="playlist-controls">
      <text wx:if="{{index==playIndex}}">正在播放</text>
    </view>
  </view>
</scroll-view>

逻辑文件index.js

data数据

data: {
    item:0,
    tab:0,
    playlist:[
    {id:1,title:'有何不可',singer:'许嵩',src:'/pages/mp3/1.mp3',coverImgUrl:'../images/m1.jpg'},
    {id:2,title:'Love Story',singer:'Taylor swift',src:'/pages/mp3/2.mp3',coverImgUrl:'../images/m2.jpg'},
    {id:3,title:'无人之岛(Cover 任然)',singer:'鸣小明',src:'/pages/mp3/3.mp3',coverImgUrl:'../images/m3.jpg'},
    {id:4,title:'云烟成雨',singer:'房东的猫',src:'/pages/mp3/4.mp3',coverImgUrl:'../images/m4.jpg'},
    {id:5,title:'小幸运',singer:'刘烨',src:'/pages/mp3/5.mp3',coverImgUrl:'../images/m5.jpg'},
    {id:6,title:'理想三旬',singer:'陈鸿宇',src:'/pages/mp3/6.mp3',coverImgUrl:'../images/m6.jpg'}
    ],
    state:'paused',
    playIndex:0,
    play:{currentTime:'00:00',duration:'00:00',percent:0,title:'',singer:'',coverImgUrl:'../images/m1.jpg'}
},
//音频对象
audioCtx:null

函数

//tab绑定事件--滑动content
changeItem:function(e){this.setData({item:e.target.dataset.item})},
//content swiper绑定事件 改变tab
changeTab:function(e){this.setData({tab:e.detail.current})},
//加载页面时执行
onReady:function(){
  this.audioCtx = wx.createInnerAudioContext();
  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.currentTime':that.formatTime(that.audioCtx.currentTime),
      'play.duration':that.formatTime(that.audioCtx.duration),
      'play.percent':that.audioCtx.currentTime/that.audioCtx.duration*100
    })
  })
  // 默认第一曲
  this.setMusic(0);
},
// 格式化时间
formatTime:function(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)
},
//根据index切换当前播放曲目
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})
},
//切换到播放列表
changePage:function(e){
  this.setData({
    item:e.currentTarget.dataset.page,
    tab:e.currentTarget.dataset.page
  })
},
//播放曲目
play:function(){
  this.audioCtx.play();
  this.setData({state:'running'})
},
//暂停曲目
pause:function(){
  this.audioCtx.pause();
  this.setData({state:'paused','play.duration':this.formatTime(this.audioCtx.duration)})
},
//播放下一首
next:function(){
  var index=this.data.playIndex>=this.data.playlist.length-1?0:this.data.playIndex+1;
  this.setMusic(index);
  this.forward(0);//此处请看下面的format函数
},
//改变进度条,改变当时播放时长
sliderChange:function(e){
  var second=e.detail.value*this.audioCtx.duration/100;
  this.forward(second);//此处请看下面的format函数
},
//单击播放列表,改变播放曲目
change:function(e){
  this.setMusic(e.currentTarget.dataset.index);
  this.forward(0);//此处请看下面的format函数
},

本人的bug

由于InnerAudioContext.seek 跳转时长不会更新,请看开发文档解决

forward:function(second){
  this.audioCtx.pause();// 先暂停
  this.audioCtx.seek(second);// 再跳转
  setTimeout(() => {this.play();}, 500);// 延时播放
})

由于路径播放不成功问题,请看路径问题使用绝对路径文章来源地址https://www.toymoban.com/news/detail-493434.html

到了这里,关于微信小程序 第三章 “音乐”小程序项目的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 移动开发技术_微信小程序_第三章测试

    作答记录 1 【单选题】下列关于小程序生命周期函数说法不正确的是( )。(5.0分) A、 分为应用生命周期函数和页面生命周期函数两种 B、 在onLoad生命周期函数中可以获得地址中的参数 C、 onLaunch生命周期函数是小程序初始化完成时触发 D、 onLaunch 生命周期函数会被重复执

    2024年02月09日
    浏览(25)
  • C++[第三章]--程序结构

    class里面的函数实现可以放到class外面实现,class里面声明即可。所以这部代码可以放到.h文件中如: 在cpp里面实现这些函数即可如: 多个cpp文件出现同名函数(非类里面的函数)会混淆。 定义:.h/.cpp文件中: 调用者源文件中: 直接使用: a::fun, a::fun2 using声明: using a::fun; // 以后

    2024年02月15日
    浏览(23)
  • 微信小程序开发实战课后习题解答————第三章(作业版)

    一、填空题 1、微信小程序中用    navigationBar      组件可以实现导航栏 2、 微信小程序中能够实现轮播效果的组件是    swiper    3、 微信小程序中实现滚动条事件的绑定方法是    bindscroll   4、 微信小程序中引入音频的组件是   InnerAudioContext   5、 微信小程序中控

    2024年02月09日
    浏览(32)
  • 第三章-Java的基本程序设计结构

      3.1一个简单的Java语言程序  这是程序虽然很简单,但是所有的Java程序都具有这种结构,因此还是值得花一些时间来研究的。首先,Java区分大小写。如果出现了大小写拼写错误(例如:将main拼写成Main),程序将无法运行。 下面逐行的查看这段源代码。pubilc称为访问修

    2024年02月03日
    浏览(32)
  • 从程序员到产品经理 第三章:用户研究和体验设计

    目录 用户研究的方法和技巧 用户行为分析和用户画像 产品设计的原则和流程

    2024年02月02日
    浏览(29)
  • 【全解析 | PTA】浙大版《Python 程序设计》题目集-第三章

    一、判断题 1.\\\'age\\\'+23不是正确的表达式。T 2 . 列表可以用find()函数来搜索数据是否在列表中。F         find()函数是字符串处理函数;Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子

    2024年04月15日
    浏览(43)
  • 南京邮电大学程序设计类教辅平台c++第三章作业编程题答案

    南京邮电大学程序设计类教辅平台c++第三章作业编程题答案 1.5.1构建一个类,含有三个数据成员,分别表示立方体的三条边长;含有构造函数(默认边长为3,2,1)和一个用来计算立方体体积的成员函数Compute()。 main()函数如下,请复制使用 代码: 2.设计一个Car类,它的数

    2023年04月20日
    浏览(33)
  • 系统集成项目管理工程师 软考中级 第三章重点汇总笔记(书本参照 第二版)

    第三章 信息系统的生命周期(p133) (1)立项阶段:即概念阶段或需求阶段,这一阶段根据用户业务发展和经营管理的需要,提出建设信息系统的初步构想;然后对企业信息系统的需求进行深入调研和分析,形成《需求规格说明书》并确定立项。 (2)开发阶段:以立项阶段所做的需

    2023年04月22日
    浏览(42)
  • 《微信小程序》音乐播放器项目

    需求:在装有node.js的机器上使用微信开发者工具开发一个音乐播放项目 写这个项目的时候电脑前后蓝屏了5次,制作不易,希望大佬们给个双击,顺子在这感谢啦! 展示: pages–index–index.js 01.png 02.png 02stop.png 03.png 04.png 05.png 06.png banner.jpg banner2.jpg banner3.jpg cover.jpg cover1.png

    2024年02月11日
    浏览(33)
  • 微信小程序仿网易音乐播放器项目

    主页样式 播放页样式 搜索页样式 排行榜页样式 小控件样式 网易云音乐API接口 后端接口,使用node写的,使用了网易云音乐API: 封装的api文件 主页面功能点 banner,滑动菜单栏采用微信的API( swiper 与 scroll-view )进行开发 滑动到底部重新获取后续的歌曲,使用onReachBottom周期

    2024年02月06日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包