##ForewordThe author is a front-end soldier , after learning small programs for a period of time, I decided to make a mobile phone software to imitate my skills. I also love music, and found that the small programs of various music platforms are relatively simple, so I chose this one. In the process of imitating learning, I also encountered many problems. After solving these problems, I also gained some gains. Today I will share with you the most difficult
Music Playback in this small program. Various problems and solutions in this part.
First of all, thank you to the api provider of this project, binaryify
Choose this project because the back-end api is provided by a big guy. When you need data, you only need to initiate some interface requests. Compare Suitable for beginners like me to get started, just write some simple front-end logic.
Since the playback page needs to deal with many things (such as the processing and display of lyrics, fast forward and rewind of the progress bar, etc.), and there are many pitfalls, in order to describe it as clearly as possible, this article It mainly focuses on introducing various operations related to
Music Playback. Details about other pages of this project will be described in detail in subsequent articles. Thank you readers for your understanding.
Project interface preview:
git addressgithub.com/shengliangg…Yuncun and video module It has not been developed yet. I will write it when I have time. This project will be updated from time to time. I will write a project usage document when I have time in the future.Official startThere are several interfaces for music playback In the request, it is almost necessary to carry the
song id. In all pages of this project, the play page exists as an independent page. When other pages jump to the play page, they will carry the song. id
Interface encapsulationThis project uses a lot of interface requests. For convenience, I encapsulate them in
utils##api.js file in the folder, and then reference the interface management file in the page. <pre class="brush:php;toolbar:false">// method(HTTP 请求方法),网易云API提供get和post两种请求方式
const GET = 'GET';
const POST = 'POST';
// 定义全局常量baseUrl用来存储前缀
const baseURL = 'http://neteasecloudmusicapi.zhaoboy.com';
function request(method, url, data) {
return new Promise(function (resolve, reject) {
let header = { //定义请求头
'content-type': 'application/json',
};
wx.request({
url: baseURL + url,
method: method,
data: method === POST ? JSON.stringify(data) : data,
header: header,
success(res) {
//请求成功
//判断状态码---errCode状态根据后端定义来判断
if (res.data.code == 200) { //请求成功
resolve(res);
} else {
//其他异常
reject('运行时错误,请稍后再试');
}
},
fail(err) {
//请求失败
reject(err)
}
})
})
}
const API = {
getSongDetail: (data) => request(GET, `/song/detail`, data), //获取歌曲详情
getSongUrl:(data) => request(GET, `/song/url`, data), //获取歌曲路径
};
module.exports = {
API: API
}复制代码</pre>Only two request APIs used on this page are shown here. You can use them by introducing them into pages that require interface requests.
Other pages jump to the play page, carrying the musicId parameter
//播放音乐
playMusic: function (e) {
let musicId = e.currentTarget.dataset.in.id // 获取音乐id
// 跳转到播放页面
wx.navigateTo({
url: `../play/play?musicId=${musicId}`
})
},复制代码
onLoad life cycle
In the
onLoad
life cycle function of play.js, get the musicId parameter passed from other pages through options, And call play()function<pre class="brush:php;toolbar:false"> /**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
const musicId = options.musicId //获取到其他页面传来的musicId
this.play(musicId) //调用play方法
},复制代码</pre>play function
##play()
The function requires a formal parameter:
musicId. This formal parameter is very important. It will be used in subsequent interface requests.
//格式化歌词
parseLyric: function (text) {
let result = [];
let lines = text.split('\n'), //切割每一行
pattern = /\[\d{2}:\d{2}.\d+\]/g;//用于匹配时间的正则表达式,匹配的结果类似[xx:xx.xx]
// console.log(lines);
//去掉不含时间的行
while (!pattern.test(lines[0])) {
lines = lines.slice(1);
};
//上面用'\n'生成数组时,结果中最后一个为空元素,这里将去掉
lines[lines.length - 1].length === 0 && lines.pop();
lines.forEach(function (v /*数组元素值*/, i /*元素索引*/, a /*数组本身*/) {
//提取出时间[xx:xx.xx]
var time = v.match(pattern),
//提取歌词
value = v.replace(pattern, '');
// 因为一行里面可能有多个时间,所以time有可能是[xx:xx.xx][xx:xx.xx][xx:xx.xx]的形式,需要进一步分隔
time.forEach(function (v1, i1, a1) {
//去掉时间里的中括号得到xx:xx.xx
var t = v1.slice(1, -1).split(':');
//将结果压入最终数组
result.push([parseInt(t[0], 10) * 60 + parseFloat(t[1]), value]);
});
});
// 最后将结果数组中的元素按时间大小排序,以便保存之后正常显示歌词
result.sort(function (a, b) {
return a[0] - b[0];
});
return result;
},复制代码
歌词去除空白行
sliceNull: function (lrc) {
var result = []
for (var i = 0; i <ol start="3">
<li><p>再接着通过id去获取歌曲的播放路径,获取到音频的数据源后,则调用<code>createBackgroundAudioManager()</code>函数,传入刚刚获取到的音频数据源。(下文详细介绍)</p></li>
<li>
<p>如果其中的任意一个环节出现了问题,则会弹出提示信息,调用tips()函数,并返回主页</p>
<h3 id="友好提示">友好提示</h3>
</li>
</ol>
The above is the detailed content of In those years, take a look at the related playback of WeChat applet imitating NetEase Cloud Music. For more information, please follow other related articles on the PHP Chinese website!
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software