微信小程序播放器的一些简单功能实现

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

微信小程序播放器的一些简单功能实现

准备工作

一、构建npm(后面用到moment的格式化时间)

微信小程序音乐播放器下一首的代码,javascript,前端,npm

二、系统后台监测程序播放器配置(系统后台要知道该播放器是否在播放歌曲,并有一些简单的业务逻辑)

在app.json中添加以下代码----和tarbar同级

"requireBackgroundModes": [
    "audio"
  ]

三、由于后面用到后台需要监测是哪首歌,需要有id和播放状态

在app.js中添加如下代码-----和onLaunch同级

GroupData:{
    MusicId:'',
    MusicPlay:false
  },

开始工作

涉及的功能介绍

1.单曲循环功能

2.上一首

3.下一首

4.播放

5.列表展示

6.进度条实时

微信小程序音乐播放器下一首的代码,javascript,前端,npm

由于本人比较懒,逐个功能介绍较复杂,直接附送源码,如看代码不明白,可直接联系本人

js部分

import moment from 'moment'

const appInstance = getApp()
Page({

  /**
   * 页面的初始数据
   */

  data: {
    isXunhuan:false,//是否开启循环
    isPlay:false, //是否正在播放
    isShow:false,  //是否展示列表
    currentTimeFomat:'0:00', //当前的时间---格式化后
    currentTime:'0', //当前时间
    durationTime:'', //总时间
    currentWidth:0 , //进度条宽度

    currentId:'001',  //当前播放歌曲id
    currentUrl:'http://music.163.com/song/media/outer/url?id=317151.mp3',  //当前播放歌曲地址
    currentTitle:'第一首',  //当前播放歌曲名称
    musicList:[{            //播放列表
      id:"001",
      url:'http://music.163.com/song/media/outer/url?id=317151.mp3',
      title:'第一首'
    },{
      id:"002",
      url:'http://downsc.chinaz.net/Files/DownLoad/sound1/201906/11582.mp3',
      title:'第二首'
    },{
      id:"003",
      url:'http://music.163.com/song/media/outer/url?id=298317.mp3',
      title:'第三首'
    }]
  },
  //点击列表按钮---展示列表页
  showList(){
    this.setData({
      isShow:true
    })
  },
  //点击别处--关闭列表页
  colseList(){
    if(this.data.isShow){
      this.setData({
        isShow:false
      })
    }
  },
  //点击列表里面的歌获得当前的歌曲
  musicListChange(e){
    this.setData({
      currentId:e.currentTarget.dataset.id,
      currentUrl:e.currentTarget.dataset.url,
      currentTitle:e.currentTarget.dataset.title,
    })
    this.musicControl(true)
  },
  //点击播放按钮
  bofang(){
    let isPlay = !this.data.isPlay  //修改播放状态
    this.musicControl(isPlay)  //传递的是已经修改了状态
  },
  //播放函数
  musicControl(isPlay){
    console.log(isPlay)
    if(isPlay){
      this.BackgroundAudioManager.src=this.data.currentUrl,
      this.BackgroundAudioManager.title=this.data.currentTitle
    }else{
      this.BackgroundAudioManager.pause()
    }
  },
  xunhuan(){
    console.log("已经开启循环播放")
    this.setData({
      isXunhuan:true
    })
  },
  xiayishou(){
    const {musicList}=this.data
    let url=''
    let title=''
    let id=''
    for(let i=0;i<musicList.length;i++){
      if(this.data.currentId==musicList[i].id){
        url=musicList[i+1].url
        id=musicList[i+1].id
        title=musicList[i+1].title
     
      }
    }
    console.log(url,id,title)
    this.setData({
      currentTitle:title,
      currentUrl:url,
      currentId :id
    })
    this.musicControl(true)
   
  },
  xunhuan2(){
    const {musicList}=this.data
    let url=''
    let title=''
    let id=''
    for(let i=musicList.length-1;i>=0;i--){
      if(musicList[i].id==this.data.currentId){
        console.log(musicList[i].id)
        url=musicList[i].url
        id=musicList[i].id
        title=musicList[i].title
      }
    }
    this.setData({
      currentUrl:url,
      currentId:id,
      currentTitle:title
    })
    this.musicControl(true)

  },
  shangyishou(){
    const {musicList}=this.data
    let url=''
    let title=''
    let id=''
    for(let i=musicList.length-1;i>=0;i--){
      if(musicList[i].id==this.data.currentId){
        console.log(musicList[i].id)
        url=musicList[i-1].url
        id=musicList[i-1].id
        title=musicList[i-1].title
      }
    }
    this.setData({
      currentUrl:url,
      currentId:id,
      currentTitle:title
    })
    this.musicControl(true)

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.BackgroundAudioManager =wx.getBackgroundAudioManager()
    
    // this.BackgroundAudioManager.src=this.data.currentUrl,
    // this.BackgroundAudioManager.title=this.data.currentTitle
    // this.BackgroundAudioManager.play()
    const MusicId=this.data.currentId
    console.log(appInstance.GroupData)
    //判断当前页面音乐是否在播放
    if (appInstance.GroupData.MusicPlay && appInstance.GroupData.MusicId === MusicId) {
      //修改当前音乐播放状态为true
      this.setData({
        isPlay: true
      })
    }
    this.BackgroundAudioManager.onPlay(() => {
      this.setData({
        isPlay: true
      })
      appInstance.GroupData.MusicPlay = true
      appInstance.GroupData.MusicId = MusicId
    })
    this.BackgroundAudioManager.onPause(() => {
      this.setData({
        isPlay: false
      })
      appInstance.GroupData.MusicPlay = false
      //由于上面必定是先播放后停止所以省略 appIstance.GroupData.MusicPlay=false
    })
    this.BackgroundAudioManager.onStop(() => {
      this.setData({
        isPlay: false
      })
      appInstance.GroupData.MusicPlay = false
    })
    this.BackgroundAudioManager.onTimeUpdate(()=>{
      let currentTimeFomat=moment(this.BackgroundAudioManager.currentTime*1000).format('mm:ss')
      //  console.log(this.BackgroundAudioManager.currentTime)
      //  console.log(this.BackgroundAudioManager.duration)

      let durationTime=Math.floor(this.BackgroundAudioManager.duration)
      let currentWidth=this.BackgroundAudioManager.currentTime/this.BackgroundAudioManager.duration*450

     if(Math.floor(this.BackgroundAudioManager.duration)==Math.ceil(this.BackgroundAudioManager.currentTime)){
       if(this.data.isXunhuan==false){
        console.log("下一首了")
        this.xiayishou()
       }else{
         console.log("因为开启了循环")
        this.xunhuan2()
       }
      
      }
      this.setData({
        currentTimeFomat,
        currentTime:Math.ceil(this.BackgroundAudioManager.currentTime),
        currentWidth,
        durationTime
      })
    });
    this.BackgroundAudioManager.onEnded(()=>{
      //进度条置为0
      this.setData({
        currentWidth:0,
        currentTime:'0'
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})

html部分

<view class="container" >
  <text class="musicName" bindtap="colseList">{{currentTitle}}</text>
  <view class="circle">
    <image src="../../image/disc.png" alt="" class="disc"></image>
    <image src="../../image/needle.png"
    class="needle"></image>  
    <image src="../../image/nvsheng.jpg"
    class="nvsheng"></image>
  </view>
  <view class="progressbar">
    <text>0:00</text>
    <view class="barprogress">
      <view class="barprogress-timely" style="width:{{currentWidth+'rpx'}}"> 
        <view class="barcircle">
        </view>
      </view>
    </view>
    <text>{{currentTimeFomat}}</text>
  </view>

  <view class="controls">
   <view class="musicControls">
    <text class="iconfont icon-xunhuanbofang" bindtap="xunhuan"></text>
    <text class="iconfont icon-shangyishou" bindtap="shangyishou"></text>
    <text class="iconfont {{isPlay?'icon-zanting':'icon-bofang'}}" bindtap="bofang"></text>
    <text class="iconfont icon-xiayishou" bindtap="xiayishou"></text>
    <text class="iconfont icon-liebiao" bindtap="showList" hover-stop-propagation="true"></text>
   
  </view>
  <view class="songList" wx:if="{{isShow}}">
      <view class="songItem" wx:for="{{musicList}}" data-url="{{item.url}}" data-id="{{item.id}}" data-title="{{item.title}}" bindtap="musicListChange">{{item.id}}--{{item.title}}</view>
    </view>
   </view> 
</view>

css部分

.container{
  padding:20rpx;
  background-color: rgba(0,0,0,0.5);
  text-align: center;
  height:100%;
}
.musicName{
  color: white;
}
.circle{
  
  position: relative;
  margin-top:40rpx;
 
}
.disc{
  position: relative;
  z-index:1;
  height:598rpx;
  width:598rpx;
}
.needle{
  position:absolute;
  left:350rpx;
  top:-20rpx;
  height:200rpx;
  width:200rpx;
  z-index:999
}
.nvsheng{
  height:370rpx;
  width:370rpx;
  position: relative;
  top:-498rpx;
  left:0rpx;
  z-index: -999;
}
/* 包裹进度条 */
.progressbar{
  position: absolute;
  bottom: 200rpx;
  height:80rpx;
  width:640rpx;
  line-height: 80rpx;
  display: flex;
}
.barprogress{
  position: relative;
  height:4rpx;
  width:450rpx;
  background: rgba(0,0,0,0.5);
  margin:auto
}
/* 真实进度条 */
.barprogress-timely{
  position: absolute;
  top:0;
  left:0;
  z-index: 1;
  height:4rpx;
  background-color: red;
} 
/* 小圆球 */
.barcircle{
  position: absolute;
  top:-4rpx;
  right:-12rpx;
  height:12rpx;
  width:12rpx;
  border-radius: 50%;
  background-color: #fff;
}

.musicControls{
  display: flex;
  height:100rpx;
  line-height: 100rpx;
  margin-top:-30rpx;
}
.musicControls text{
  width:20%;
 font-size: 50rpx;
}
.musicControls text:nth-child(3)
{
  font-size: 65rpx;
}
.songList{
  position:fixed;
  top:400rpx;
  right:20rpx;
  width: 94%;
  background-image: linear-gradient(120deg, #f093fb 0%, #f5576c 100%);
  border-radius: 20rpx;
  z-index: 10000;
  height:calc(100vh - 400rpx)
}
.songItem{
    height:100rpx;
    line-height: 100rpx;
    color: white;

}

内附三张图片------右键可直接拿走

微信小程序音乐播放器下一首的代码,javascript,前端,npm微信小程序音乐播放器下一首的代码,javascript,前端,npm微信小程序音乐播放器下一首的代码,javascript,前端,npm

播放器字体图标的引入------这边就不放出来了

有需要的可以自行到阿里图标库下载相关字体
[[iconfont-阿里巴巴矢量图标库](https://www.iconfont.cn文章来源地址https://www.toymoban.com/news/detail-818228.html

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

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

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

相关文章

  • uniapp - 微信小程序接入腾讯视频播放器功能插件,uniapp开发微信小程序端调用引入并使用腾讯视频播放组件完整全流程(详细示例源码,一键复制开箱即用)

    在uniapp 微信小程序项目中,集成腾讯视频功能插件,实现播放腾讯视频效果,附带详细示例源码及注释, 你可以跟着步骤一步步来,保证几分钟就能快速在uniapp小程序项目中植入腾讯视频功能!

    2024年02月12日
    浏览(37)
  • 《微信小程序》音乐播放器项目

    需求:在装有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日
    浏览(35)
  • 微信小程序音乐播放器实践

    1.成品展示: 实现搜索音乐,同步歌词,控制播放等功能 2.设计: 采用微信开发文档中的audio组件的作为代码原型,进行扩展,链接audio | 微信开放文档 (qq.com) 3、具体设计 思维导图 链接:https://pan.baidu.com/s/1whZC2xOP4HvbDMjMPA7pRQ  提取码:ljsb 3.1 播放界面index 3.2 索引界面list

    2024年02月11日
    浏览(34)
  • 微信小程序——【云音乐播放器】

    目录 第一章 开发准备 一、项目结构 二、新建微信小程序项目 第二章 标签页切换 一、常用组件介绍 二、编写页面结构和样式 第三章 音乐推荐 一、组件介绍 二、编写音乐推荐页面结构和样式 第四章 播放器 一、任务分析 二、组件介绍 三、实现播放器功能 四、编写播放器

    2024年02月09日
    浏览(34)
  • 音乐播放器微信小程序

    一:学习目标: 掌握swiper组件、scroll-view组件的使用; 掌握image组件的使用; 掌握slider组件的使用; 掌握音频API的使用;  二:目录: 1. 开发前的准备 1.1 音乐小程序 项目展示 : 1.1.1: 音乐推荐 界面展示: 1.1.2: 播放器 界面展示: 1.1.3: 播放列表 界面展示: 1.2: 项目

    2024年02月05日
    浏览(60)
  • 微信小程序音乐播放器【含源码】

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

    2024年02月06日
    浏览(28)
  • 微信小程序实现音乐播放器(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绪论 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)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包