微信小程序音乐播放器实践

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

音乐播放器

1.成品展示:

实现搜索音乐,同步歌词,控制播放等功能

微信小程序音乐播放器实践

微信小程序音乐播放器实践

2.设计:

采用微信开发文档中的audio组件的作为代码原型,进行扩展,链接audio | 微信开放文档 (qq.com)

微信小程序音乐播放器实践

3、具体设计

微信小程序音乐播放器实践

思维导图 链接:https://pan.baidu.com/s/1whZC2xOP4HvbDMjMPA7pRQ 
提取码:ljsb

3.1 播放界面index

<view class="page" >
<view class="lyrics_view" class="container" style="margin-top: 0;">
<scroll-view  scroll-y="true" style="height:500rpx;" bindscrolltoupper="upper"
bindscrolltolower="lower" scroll-top="{{scrollTop}}">
<view class="lyric" wx:for="{{lyrics}}" wx:for-item="lyric" wx:for-index="index" wx:key="lyric">
<text class="{{currentIndex==index?'focus_view':''}}">{{lyric}}</text>
</view>
</scroll-view>
 </view>
  <view class="page__bd">
      <audio class="section_gap" src="{{album.src}}" poster="{{album.poster}}" name="{{album.name}}" author="{{album.author}}" action="{{audioAction}}" 
      bindplay="audioPlayed" bindtimeupdate="audioTimeUpdated" controls></audio>
  </view>
​
  <view class="section__tit">
    <text class="section__title">进度</text>
    <view class="body-view">
      <slider bindchange="timeSliderChanged" left-icon="cancel" right-icon="success_no_circle"/>
    </view>
  
​
    <!-- <text class="section__title">暂停</text> -->
      <button class="section_gap" bindtap="pauseAudio" style="width:50vw;height: 15vw;border-radius: 25rpx;">
        <image class="tapimg1" style="width: 30px;height: 30px;"
        src="{{myMethod=='play'?'../images/play.png':'../images/pause.png'}}"></image>
      </button>
  </view>
</view>
​
Page({
  // overflow: hidden,
  data: {
    currentIndex:-1,
    music:[],
    lyrics:[],
    myMethod:'pause',
    album: {
      poster: '',
      name: '',
      author: '',
      src: '',
    },
    audioAction: {
      method: 'pause'
    }
  },
  onLoad:function(param){
    //接收传入的歌曲对象信息,供页面audio组件展示使用
    var alb=JSON.parse(param.album);
    let songId = alb.id
    alb.src=`http://music.163.com/song/media/outer/url?id=${songId}.mp3`;
    console.log(alb);
    this.setData(
      {album:alb}
    );
    //获取歌词
    this.getLyric(songId);
    console.log(this.data.album);
  },
  getLyric:function(songId){
    let parent = this
    wx.request({
      url: `https://music.163.com/api/song/lyric?os=pc&id=${songId}&lv=-1&kv=-1&tv=-1`,
      success:function(response){
         let result =response.data.lrc.lyric;
         //歌词数组局部变量
         let localLyrics =[]
         //歌词字符串按\n进行切割
         let la = result.split("\n");
         //可能存在歌词为空格的音轨
         for(let i=0;i<la.length;i++){
           let temp = la[i].split("]");
           if(temp.length == 2 && temp[1] != ""){
             let time =temp[0].slice(1).split(":");
             let minute =parseFloat(time[0]);
             let second =parseFloat(time[1]);
            localLyrics.push([(minute*60+second).toFixed(3),temp[1]]);
           }
         }
         let temp=[];
         for(let i =0;i<localLyrics.length;i++)
         {
           temp[i]=localLyrics[i][1]
         }
         parent.setData({
           lyrics:temp,
           music:localLyrics
         });
         
​
  }
  })
},
  //音频更新事件
  audioTimeUpdated: function (e) {
    this.duration = e.detail.duration;
    let curTime = e.detail.currentTime;
    let localLy = this.data.music;
    for(let i=0;i<localLy.length;i++){
      if(i==localLy.length-1&&curTime>=localLy[i][0]){
          this.setData({currentIndex:i})
      }
      if(curTime>localLy[i][0]&&curTime<localLy[i+1][0]){
        this.setData({currentIndex:i});
      }
    }
   },
  audioPlayed: function (e) {
    console.log('audio is played')
  },
  timeSliderChanged: function (e) {
    if (!this.duration)
      return;
    var time = this.duration * e.detail.value / 100;
    this.setData({
      audioAction: {
        method: 'setCurrentTime',
        data: time
      }
    });
  },
  playbackRateSliderChanged: function (e) {
    this.setData({
      audioAction: {
        method: 'setPlaybackRate',
        data: e.detail.value
      }
    })
  },
  pauseAudio: function () {
    if(this.data.audioAction.method=="pause"){
      this.setData({
        audioAction: {
          method: 'play'
        },
        myMethod:'play'
      });
    }else{
      this.setData({
        audioAction: {
          method: 'pause'
        }
        ,myMethod:'pause'
      });
    }
  }
})
/* songs/songs.wxss */
page{
  height:100%;
  background-color: #ffebcd; 
}
.focus_view{
  background-color: rgb(241, 201, 20);
  font:bold 20px Arial
}
.music{
  display: flex;
}
.lyric{
  text-align: center;
}
.page{
  background-color: blanchedalmond;
  height: 320px;
}
.section_gap{
  background: #deb887;
}

3.2 索引界面list文章来源地址https://www.toymoban.com/news/detail-501343.html

<view class="all">
<!-- 搜索框 -->
<view class="mySearch">
<input type="text" model:value="{{inputValue}}" placeholder="请输入歌手名或者歌曲名"/>
<button style="width:25vw;" size="mini" bindtap="myButton">搜索</button>
</view>
<!--id ,name,poster,artists(可能会存在多个歌手) -->
<view  class="listview" wx:for="{{songs}}" wx:key="song" bindtap="navigateToMusic" data-albumId="{{item.id}}" data-albumName="{{item.name}}">
<!--海报图片-->
<image src="{{posterImage}}"></image>
<!-- 歌曲的名称-->
<view>{{item.name}}</view>
<!--歌唱家(可能多个)-->
<view  wx:for="{{item.artists}}" wx:key="artist" wx:for-item="artist" wx:for-index="index">
<view class="{{index==0?'artists':''}}" >
<text>{{index==0?artist.name:'&'+artist.name}}</text>
</view>
</view>
</view>
</view>
Page({
  data:{
    songs:[],
    inputValue:'',
    posterImage:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F-fo3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2Fb21c8701a18b87d6416a3095060828381f30fd14.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661942984&t=4e2d5aa85e2d7f6c22c22ffa15093bde',
    album:{
      id:'',
      name:'',
      artists:''
    }
  },
​
    // navigateToMusic:function(e){
    //   this.setData({
    //     id:e.currenTarget.dataset.albumid,
    //     src:`http://music.163.com/song/media/outer/url?id${e.currenTarget.dataset.albumid}.mp3`,
     //    name:currenTarget.dataset.albumname,
     //    srtist: ''
     //  });
      navigateToMusic:function(e){
        this.setData({
          album:{
            id:e.currentTarget.dataset.albumid,
            name:e.currentTarget.dataset.albumname,
            artist: ''
          }
        })
      
      console.log(this.data.album);
      wx.navigateTo({
        //url:' /index/index?album='+this.data.album,
        url:'/songs/songs?album='+JSON.stringify(this.data.album),
       })
  },
  // mySearch:function(e){
  //   this.setData({inputValue:e.detail.value})
  // },
 myButton:function(){
   this.searchMusic(this.data.inputValue)
 },
​
 onLoad:function(){
    this.searchMusic('许嵩')
 },
 searchMusic:function(searchName){
  let parent = this;
  wx.request({
   url: `https://music.163.com/api/search/get?s=${searchName}&type=1&limit=10`,
   success:function(response){
      console.log(response.data.result.songs);
      parent.setData({
        songs:response.data.result.songs
      });
   }
 })
 }
})
.all{
  background-color: blanchedalmond;
}
.listview{
  display: flex;
  margin-bottom: 5px;
  border-bottom: solid rgb(247, 194, 22);
  border-radius: 20px;
}
.listview image{
  width: 150rpx;
  height: 150rpx;
  border-radius: 50%;
}
.mySearch{
  display: flex;
  margin-bottom: 20rpx;
  border-bottom: solid rgb(247, 162, 5);
}
.mySearch input{
  /* display: flex; */
  margin-top: 20rpx;
  background: #deb887;
  width: 70%;
  height: 100rpx;
  font:bold 15px Arial;
  margin-left: 15rpx;
  margin-bottom: 40rpx;
  /* 文字居中 */
  text-align: center;
  border-radius: 25rpx;
}
.mySearch button{
  margin-top: 20rpx;
  height: 100rpx;
  /* 行内高,可将按钮文字居中 */
  line-height: 100rpx;
  border-radius: 25rpx;
  background-color: #deb887;
  color: rgb(15, 15, 15);
  width: 300rpx;
}
.artists{
  margin-left: 10px;
}

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

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

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

相关文章

  • 微信小程序音乐播放器【含源码】

    微信小程序音乐播放器 取源码私聊

    2024年02月06日
    浏览(27)
  • 微信小程序实现音乐播放器(1)

    代码涉及的主要文件有: app.json app.wxss pages/music/music.json pages/music/music.wxml pages/music/music.wxss pages/music/music.js 另外,你可能需要的图片资源,在这里! BackgroundAudioManager实现背景音乐 imzusheng / netease-music-uniapp

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

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

    2024年02月06日
    浏览(27)
  • 微信小程序实现一个音乐播放器的功能

    1.页面包含一个音乐列表,点击列表中的音乐可以播放对应的音乐。 2.播放中的音乐在列表中有标识,并且可以暂停或继续播放。 3.显示当前音乐的播放进度和总时长,并可以拖动进度条调整播放进度。 4.点击切换按钮可以切换到下一首音乐。 5.点击循环按钮可以切换音乐的

    2024年01月22日
    浏览(38)
  • 基于微信小程序的音乐播放器设计

    目 录 1绪论 1 1.1选题背景及意义 1 1.2发展现状 1 1.2.1什么是微信小程序 1 1.2.2小程序市场的现状 4 1.3研究主要内容 4 2系统技术 5 2.1 Java语言 5 2.2 SSM框架 6 2.3 Vue.js框架 7 2.4 Eclipse开发工具 8 2.5数据库 9 2.6系统开发环境概述 10 3系统分析 12 3.1 功能需求(用例图分析) 12 3.1.1 网络音

    2024年02月11日
    浏览(28)
  • java微信小程序音乐播放器分享系统

    随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,音乐播放器小程序被用户普遍使用,为方便用户能够可以随时进行音乐播放器小程序的数据信息管理,特开发了基于音乐

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

    uniapp 仿照网易云播放器功能

    2024年02月12日
    浏览(71)
  • ssm基于微信小程序的音乐播放器(程序+开题)

    本系统(程序 + 源码)带文档 lw 万字以上 文末可获取一份本项目的 java 源码和数据库参考。 研究背景: 随着移动互联网的普及和智能手机的广泛应用,人们对于音乐的需求也越来越高。传统的音乐播放器存在着功能单一、操作繁琐等问题,无法满足用户的需求。因此,开发

    2024年02月02日
    浏览(33)
  • 微信小程序——实现音乐播放器(上下切换歌曲、进度条拉动、暂停与继续播放)

    小伙伴们你们有没有想过自己搞一个播放器,播上自己喜欢的歌单,那是多么的惬意啊~ 之前,小编遇到一个项目,语音导览的播放器。其实跟播放歌单一个道理。 但是一看微信开发文档里面的音频API又是那么多,我们该如何选择呢?在这里小编选择了使用wx.createAudioContext

    2024年02月02日
    浏览(46)
  • 案例115:基于微信小程序的音乐播放器的设计与实现

    文末获取源码 开发语言:Java 框架:SSM JDK版本:JDK1.8 数据库:mysql 5.7 开发软件:eclipse/myeclipse/idea Maven包:Maven3.5.4 小程序框架:uniapp 小程序开发软件:HBuilder X 小程序运行软件:微信开发者 目录 目录 前言 系统展示 系统首页界面的设计实现 用户注册功能的设计实现 用户登

    2024年02月02日
    浏览(30)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包