当前位置:网站首页>Improvement of wechat applet 26 playing music page ②
Improvement of wechat applet 26 playing music page ②
2022-07-25 19:13:00 【Mu Quanyu [dark cat]】
26.1 Realize the next song and the previous song
Interface : http://localhost:3000/song/detail?ids=347230 Get music details , In this case, we only need to get musicId You can get All the information about this song .
- Encapsulate a method to obtain songs and update song information
// Get the details of a single music
async getMusicDetail(musicId) {
//http://localhost:3000/song/detail?ids=347230
let data = await request("song/detail", {
ids: musicId});
let song = data.songs[0];
this.setData({
song
})
},
- direct Go to call Related methods Can achieve The function of . Our approach In fact, it has been written .
// Click the callback of cutting song
handleSwitch(event){
// 1. PubSubJS Implementation method
// Get the type of cut song
let type = event.currentTarget.id;
PubSub.publish('switchType', type);
PubSub.subscribe('musicId', (msg, musicId) => {
this.setData({
isPlay: true
});
this.getMusicDetail(musicId);
this.musicControl(this.data.isPlay,musicId);
});
},
26.2 Static build playback progress bar
<view class="songDetailContainer">
<view class="author">{
{song.ar[0].name}}</view>
<view class="circle"></view>
<image class="needle {
{isPlay? 'needleRotate':''}}" src="/static/images/song/needle.png"></image>
<view class="discContainer {
{isPlay? 'discRotate':''}}" >
<image class="disc" src="/static/images/song/disc.png"></image>
<image class="musicImg" src="{
{song.al.picUrl}}"></image>
</view>
<!-- Progress bar control area -->
<view class="progressControl">
<text>00:00</text>
<!-- General progress bar -->
<view class="barControl">
<!-- Real time progress bar -->
<view class="audio-currentTime-Bar">
<!-- Little ball -->
<view class="audio-circle"></view>
</view>
</view>
<text>03:00</text>
</view>
<!-- The bottom controls the playback area -->
<view class="musicControl">
<text class="iconfont icon-zhongbo"></text>
<text id="pre" class="iconfont icon-shangyishoushangyige" bindtap="handleSwitch"></text>
<text class="iconfont {
{isPlay? 'icon-zanting':'icon-bofang'}} big" bindtap="handleMusicPlay"></text>
<text id="next" class="iconfont icon-xiayigexiayishou" bindtap="handleSwitch"></text>
<text class="iconfont icon-24gl-playlistMusic5"></text>
</view>
</view>
/* Progress bar control area */
.progressControl {
width: 640rpx;
height: 80rpx;
line-height: 80rpx;
line-height: 80rpx;
padding-bottom: 200rpx;
display: flex;
}
.barControl {
position: relative;
width: 450rpx;
height: 4rpx;
background: rgba(0,0,0,0.4);
margin: auto;
}
.audio-currentTime-Bar {
position: absolute;
top: 0;
left: 0;
width: 100rpx;
z-index: 1;
height: 4rpx;
background: red;
}
/* Little ball */
.audio-circle {
position: absolute;
right: -12rpx;
top: -5rpx;
width: 12rpx;
height: 12rpx;
border-radius: 50%;
background: #fff;
}
26.3 Dynamically display the relevant contents of the progress bar
- Dynamic display time
<!-- Progress bar control area -->
<view class="progressControl">
<text>{
{curretTime}}</text>
<!-- General progress bar -->
<view class="barControl">
<!-- Real time progress bar -->
<view class="audio-currentTime-Bar">
<!-- Little ball -->
<view class="audio-circle"></view>
</view>
</view>
<text>{
{durationTime}}</text>
</view>
- Use Third party date processing class library Moment.js




- Encapsulate an acquisition Function of total playback time
// Get the playback time
getTime(song){
let durationTime = moment(song.dt).format('mm:ss');
this.setData({
durationTime
})
},
- Add one to update monitor
// The progress of the music manager's real-time playback
appInstance.globalData.backgroundAudioManager.onTimeUpdate(() => {
let currentTime = moment(appInstance.globalData.backgroundAudioManager.currentTime * 1000).format('mm:ss');
if(this.data.isPlay && this.data.song.id == this.data.preMusicId){
this.setData({
currentTime
});
}
});
- final js Code ( contain Repair Before Of Playing problems . You can use this code .)
// pages/songDetail/songDetail.js
import request from "../../utils/request";
import PubSub from "pubsub-js";
import moment from 'moment';
const appInstance = getApp();
Page({
/** * Initial data of the page */
data: {
isPlay: false, // Identify whether the music is played
song: {
},
preMusicId: "", // Music played before ID
currentTime: "00:00", // Real time
durationTime: "00:00" // Total duration
},
// Click the callback of cutting song
handleSwitch(event){
// 1. PubSubJS Implementation method
// Get the type of cut song
let type = event.currentTarget.id;
PubSub.publish('switchType', type);
PubSub.subscribe('musicId', (msg, musicId) => {
this.setData({
isPlay: true
});
this.getMusicDetail(musicId);
this.musicControl(this.data.isPlay,musicId);
});
},
// Get the details of a single music
async getMusicDetail(musicId) {
//http://localhost:3000/song/detail?ids=347230
let data = await request("song/detail", {
ids: musicId});
let song = data.songs[0];
// Get the total playing time
this.getTime(song);
this.setData({
song
})
},
// Get the total playing time
getTime(song){
let durationTime = moment(song.dt).format('mm:ss');
this.setData({
durationTime
})
},
// Click play / Suspended Callback function
handleMusicPlay(){
this.setData({
isPlay:!this.data.isPlay
});
this.musicControl(this.data.isPlay,this.data.song.id)
},
// Control music playback / Suspended Function function
async musicControl(isPlay, musicId) {
console.log(musicId + "||" + this.data.preMusicId);
if (isPlay) {
// The music starts playing
if(this.data.preMusicId == musicId || musicId == appInstance.globalData.musicID){
appInstance.globalData.backgroundAudioManager.play();
}else{
// Get music playback link
appInstance.globalData.backgroundAudioManager.stop();
let musicLinkData = await request('song/url', {
id: musicId});
appInstance.globalData.backgroundAudioManager.title = this.data.song.name;
appInstance.globalData.backgroundAudioManager.src = musicLinkData.data[0].url;
}
} else {
// Pause play
appInstance.globalData.backgroundAudioManager.pause();
}
this.setData({
preMusicId: musicId
})
},
/** * Life cycle function -- Monitor page loading */
onLoad(options) {
// Get event object
let that = this;
const eventChannel = this.getOpenerEventChannel();
eventChannel.on('song', function(data) {
that.setData({
song: data
})
});
// Get the total playing time
this.getTime(this.data.song);
wx.setNavigationBarTitle({
title: this.data.song.name
});
if(appInstance.globalData.isMusicPlay && this.data.song.id === appInstance.globalData.musicID){
this.handleMusicPlay();
}
// Music manager plays monitor
appInstance.globalData.backgroundAudioManager = wx.getBackgroundAudioManager();
appInstance.globalData.backgroundAudioManager.onPlay(() => {
this.setData({
isPlay: true
});
appInstance.globalData.musicID = this.data.preMusicId;
appInstance.globalData.isMusicPlay = true;
});
// Music Manager pauses listening
appInstance.globalData.backgroundAudioManager.onPause(() => {
this.setData({
isPlay: false
})
appInstance.globalData.musicID = this.data.preMusicId;
appInstance.globalData.isMusicPlay = true;
});
// Music managers stop listening
appInstance.globalData.backgroundAudioManager.onStop(() => {
this.setData({
isPlay: false
})
appInstance.globalData.musicID = this.data.preMusicId;
appInstance.globalData.isMusicPlay = false;
});
// The progress of the music manager's real-time playback
appInstance.globalData.backgroundAudioManager.onTimeUpdate(() => {
let currentTime = moment(appInstance.globalData.backgroundAudioManager.currentTime * 1000).format('mm:ss');
if(this.data.isPlay && this.data.song.id == this.data.preMusicId){
this.setData({
currentTime
});
}
});
},
/** * Life cycle function -- Listening page first rendering completed */
onReady() {
},
/** * Life cycle function -- Monitor page display */
onShow() {
},
/** * Life cycle function -- Monitor page hidden */
onHide() {
},
/** * Life cycle function -- Monitor page uninstall */
onUnload() {
},
/** * Page related event handler -- Monitor user pull-down action */
onPullDownRefresh() {
},
/** * Handling function of page pull bottom event */
onReachBottom() {
},
/** * Users click the upper right corner to share */
onShareAppMessage() {
}
})

边栏推荐
- Full scale and Xuan of C key
- 【DETR用于3D目标检测】DETR3D: 3D Object Detection from Multi-view Images via 3D-to-2D Queries
- 21 days proficient in typescript-4 - type inference and semantic check
- 歌曲转调之后和弦如何转换
- 有孚网络受邀参加2022全国CIO大会并荣获“CIO信赖品牌”称号
- “未来杯”第二届知识图谱锦标赛正式启动
- 微信小程序 29 热搜榜的完善②
- MES管理系统有什么应用价值
- Fruit chain "siege": it's a journey of sweetness and bitterness next to apples
- 阿里云免费SSL证书申请详细流程
猜你喜欢

Analysis of the internet jam in IM development? Network disconnection?

房企打响“保交战”

MySQL sub query (selected 20 sub query exercises)

Real estate enterprises have launched a "war of guarantee"

CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...

Baklib:制作优秀的产品说明手册

微信小程序 26 播放音乐页的完善②

“未来杯”第二届知识图谱锦标赛正式启动
![[919. Complete binary tree inserter]](/img/d9/15a9af50893db955d9ebb4d7d4e3d1.png)
[919. Complete binary tree inserter]

Alibaba cloud free SSL certificate application detailed process
随机推荐
一个函数中写多少行代码比较合适呢? 代码整洁之道
Baklib: make excellent product instruction manual
无惧高温暴雨,有孚网络如何保您无忧?
What is the application value of MES management system
网上商城系统MySql数据库设计项目实战
小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
In the first half of the year, the shipment volume has exceeded that of the whole year of last year, and centritec millimeter wave radar has "captured" the international giant
【919. 完全二叉树插入器】
JS 基本类型 引用类型 深/浅克隆复制
Software testing (mind mapping)
srec_cat 常用参数的使用
Hongmeng - Damiao computing Sketchpad - Introduction
Juzhi cloud computing opens a new era to the "proprietary cloud" of Youfu network
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
微信小程序 27 进度条的动态实现和搜索框、热搜榜的静态搭建
[applet development] detailed explanation of host environment
GDB help
[cloud native kubernetes] management of secret storage objects under kubernetes cluster
Baklib:制作优秀的产品说明手册
Ping command details [easy to understand]