Home Web Front-end JS Tutorial JS implements music player interface

JS implements music player interface

Mar 23, 2018 pm 04:36 PM
javascript player music

This time I will bring you JS to implement the music player interface. What are the precautions for JS to implement the music player interface? The following is a practical case, let's take a look.

This article introduces an example of implementing a music player on a vue page and shares it with everyone. The details are as follows:

The effect is as follows:

Project address: https://github.com/ermu592275254/MiniMusicPlayer

Demo address: https://ermu592275254.github.io/MiniMusicPlayer/(song link has expired)

Conception before development

Interface

To be a music player, the interface must be cool. It's so low that I don't feel anything when listening to the music. It is intended to be used at work, so I made an interface similar to NetEase Cloud Music with a suitable size. No need to be compatible with mobile phones.

Use css to make icons

This paper considers simple and practical needs. The icons can be SVG, url or css. Compared to url, SVG and css are better. For the sake of practice, I finally chose css. Making good use of after and before can reduce a lot of DOM nesting.

.next {
  position: relative;
  display: inline-block;
  height: 36px;
  width: 36px;
  border: 2px solid #fff;
  border-radius: 20px;
  -webkit-border-radius: 20px;
  -moz-border-radius: 20px;
}
    
.next:before {
  content: '';
  height: 0;
  width: 0;
  display: block;
  border: 10px transparent solid;
  border-right-width: 0;
  border-left-color: #fff;
  position: absolute;
  top: 8px;
  left: 10px;
}
.next:after {
  content: '';
  height: 20px;
  width: 4px;
  display: block;
  background: #fff;
  position: absolute;
  top: 8px;
  left: 22px;
}
Copy after login

draw a record

NetEase Cloud’s record is very beautiful, I want to make a record too! With good use of box-shadow, one element can be made into a beautiful record record effect.

.disc {
  position: relative;
  margin-top: 10%;
  margin-left: 10%;
  width: 300px;
  height: 300px;
  border-radius: 300px;
  transform: rotate(45deg);
  background-image: radial-gradient(5em 30em ellipse, #fff, #000);
  border: 2px solid #131313;
  box-shadow: 0 0 0 10px #343935;
  opacity: 0.7;
}
Copy after login

Use range to make progress bar

The style of audio itself is ugly, and the effects presented by different browsers are also different. Of course, you can modify the style of audio. The traditional method is to hide audio through the controls attribute, and then use p instead. Now is the HTML5 era. Of course, we must use a new element that is more suitable for the scene——range.

input[type=range] {
  -webkit-appearance: none;
  width: 80%;
  height: 8px;
  border-radius: 10px;
  background-color: #fff;
}
input[type=range]::-webkit-slider-thumb{
  -webkit-appearance: none;
} 
input[type=range]::-webkit-slider-runnable-track {
  height: 8px;
  border-radius: 20px;
}
input[type=range]:focus {
  outline: none;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  margin-top: -3px;
  height: 14px;
  width: 14px;
  background: #eb7470;
  border-radius: 50%;
  border: solid 3px #fff;
  box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.5);
}
Copy after login

Background filter blur

It feels great to set the picture as the background. It can be said that the background provides half of the appearance of the entire player. The setting is also very simple, using css3 filters.

.bg-blur {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
  height: 100%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  filter: blur(20px);
  z-index: -1;
}
Copy after login

The background image is controlled through js.

<p class="bg-blur" :style="`background-image:url(${currentSong.album_logo})`"></p>
Copy after login

Song resources

Climb down the interface

Go directly to the Xiami official website to open the network and copy the url to postman Make requests. By modifying the headers, it is found that the Referer will be verified. In other words, only domain names allowed by Xiami can access this interface. http://api.xiami.com/web?v=2.0&app_key=1&key=aliez&page=1&limit=5&callback=jsonp154&r=search/songs

solve cross-domain issues

Because the interface supports jsonp. At first, I tried to set the chrome browser to cross domain, and then made a jsonp request through $.ajax. Can be accessed normally.

It suddenly stopped working. Did Xiami impose restrictions?

So I used node to start a service, fake the referer to initiate a request, and then forward the request to the page. I accidentally wrote an agent.

...
case '/song':
  let songOptions = {
    url: 'http://api.xiami.com/web?'+ urlArr[1],
    headers: {
      'Referer': 'http://m.xiami.com/'
    }
  };
  function callback1(error, response, body) {
    if (!error && response.statusCode == 200) {
      res.end(body);
    }
  }
  request(songOptions, callback1);
  break;
...
Copy after login

Lyrics scrolling

As a high-quality player, lyrics scrolling is a must.

Principle

Save each lyric as an object with corresponding time. When the current duration of the song is greater than or equal to the time of the lyrics and less than the time of the next line of the lyrics, then scroll the lyrics to the visual area. And change the font color.

Formatted Lyrics

The lyrics returned by the interface were confusing. After careful study, I found that they were regular.

[ti:aLIEz]
[ar:SawanoHiroyuki[nZk]:mizuki]
[al:o1]
[ly:澤野弘之]
[mu:澤野弘之]
[ma:]
[pu:]
[by:ttpod]
[total:268512]
[offset:0]
[00:00.000]<195>aLIEz <199>- <451>SawanoHiroyuki[nZk]:mizuki
[x-trans]彻头彻尾的谎言 - SawanoHiroyuki[nZk]:mizuki
[00:01.095]<201>作<250>詞<200>:<201>澤<200>野<199>弘<300>之
[x-trans]
[00:02.846]<200>作<150>曲<150>:<200>澤<200>野<351>弘<349>之
[x-trans]
[00:20.828]<200>決<250>め<200>つ<201>け<149>ば<201>か<349>り
[x-trans]一直独断专权
[00:23.279]<200>自<200>惚<200>れ<200>を<200>着<400>た
[x-trans]总是自负逞强
[00:24.979]<200>チ<200>ー<200>プ<450>な<550>hokori<350>で
[x-trans]明明只是一文不值的骄傲
......
  refactoringLyrics(lyric){
  let text = lyric.split('[offset:0]')[1];
  let textArr = text.split('\n');
  let lyricsArr = [], translate = [];
  textArr.forEach((item, index) => {
    let time = 0, text = '';
    if (item.indexOf('[x-trans]') > -1) {
      translate.push(item.split('[x-trans]')[1])
    } else if (item.trim() != '') {
      time = item.slice(1, 6).split(':');
      time = parseInt(time[0]) * 60 + parseInt(time[1]);
      text = item.slice(11);
      let arr = text.split('>');
      let str = arr.reduce((a, b) => {
        return a.split('<&#39;)[0] + b.split(&#39;<&#39;)[0]
      });
      let obj = {
        time: time,
        text: str
      };
      lyricsArr.push(obj);
    }
  });
  for (let i in translate) {
    lyricsArr[i].text = lyricsArr[i].text + &#39;\n&#39; + translate[i];
  }
  this.currentLyrics = lyricsArr;
},
Copy after login

Search bar implementation

Subcomponent mounting under the same file

In order to follow modular development, it was decided to add the search bar Written as a subcomponent. When writing subcomponents on the same page, it is important to mount the subcomponents to the corresponding template. This template cannot be included by the mounting element of the parent component, otherwise the parent component will report undefined because it cannot render the data in the child component.

<p id="app" class="main">
...
</p>
<template id="search-box">
...
</template>
var searchBox = {
    template: '#search-box',
    props: {
      isShow: Boolean,
      openFun: Function
    },
    data(){
      return {
        resultList: [],
        searchValue: '',
      }
    },
    methods: {
    }
  };
 new Vue({
  el: '#app',
  components: {
    'com-tip': tip,
    'search-box': searchBox
  },
  ...
})
Copy after login

eventBus solves data transmission

通过jsonp去请求数据,需要设置一个callback函数,此callback写成一个全局函数,如果不这样写,而是通过 searchBox.methods.callback的形式,this指向将为methods。而无法直接给searchBox的data赋值。 于是通过eventBus来处理,这样更易维护。

var EventBus = new Vue();
var callBack = function(result) {
  console.log(result);
  EventBus.$emit('callBack', result);
};
...
mounted(){
  let self = this;
  EventBus.$on('callBack', function(res) {
    if (res && res.data) {
      self.resultList = res.data.songs;
    }
  })
}
...
Copy after login

localStrong储存歌曲信息

下次再打开,应该播放列表应该保留上一次的数据,这个可直接用localstrong实现

踩了坑

prop传递数据

使用cdn,vue的prop只支持中线格式,驼峰格式不生效

ps: 在用webpack打包的项目中用驼峰是可以,在打包过程中,会做处理。

// 正确写法
<search-box :is-show="showSearch" :open-fun="openSearch" @push-song="pushNewSong"
        @play-song="playSong"></search-box>
// 错误写法
<search-box :isShow="showSearch" :openFun="openSearch" @pushSong="pushNewSong"
        @playSong="playSong"></search-box>
Copy after login

待优化

手动修改进度,偶尔会不生效。

搜索暂不支持分页

不支持建歌单

背景颜色与进度条颜色相近需修改进度条颜色

不支持播放模式选择-单曲循环-随机播放

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Express与Koa2的使用详解

地图网格的实现

The above is the detailed content of JS implements music player interface. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
Bose Soundbar Ultra launch experience: Home theater right out of the box? Bose Soundbar Ultra launch experience: Home theater right out of the box? Feb 06, 2024 pm 05:30 PM

For as long as I can remember, I have had a pair of large floor-standing speakers at home. I have always believed that a TV can only be called a TV if it is equipped with a complete sound system. But when I first started working, I couldn’t afford professional home audio. After inquiring and understanding the product positioning, I found that the sound bar category is very suitable for me. It meets my needs in terms of sound quality, size and price. Therefore, I decided to go with the soundbar. After careful selection, I selected this panoramic soundbar product launched by Bose in early 2024: Bose home entertainment speaker Ultra. (Photo source: Photographed by Lei Technology) Generally speaking, if we want to experience the "original" Dolby Atmos effect, we need to install a measured and calibrated surround sound + ceiling at home.

VLC Chromecast not working on Windows PC VLC Chromecast not working on Windows PC Mar 26, 2024 am 10:41 AM

Is the VLC Chromecast feature not working on your Windows PC? This issue may be caused by compatibility issues between your Chromecast device and VLC’s casting feature. In this article, we will tell you what you can do in this situation and what to do if VLC renderer cannot find your Chromecast. How to use ChromecastVLC on Windows? To use VLC to cast videos from Windows to Chromecast, follow these steps: Open the media player app and go to the play menu. Navigate to the Renderer option and you will be able to see the Chromecast device detected

Audacity now gets AI audio editing capabilities with Intel OpenVINO plug-in Audacity now gets AI audio editing capabilities with Intel OpenVINO plug-in Feb 15, 2024 am 11:06 AM

Audacity is a free and open source cross-platform audio editing software. It has an open code and plug-in contribution mechanism, and anyone can participate. In addition, Intel offers a free set of OpenVINOAI plug-ins designed for music editors and podcast producers. This website noticed that the plug-in package is about 2GB in size and can be downloaded on Intel's GitHub page. It also requires the 64-bit Windows version of Audacity to run. The most intuitive thing about this AI plug-in is that it brings three advanced tools to the Audacity music editing function: The first is the "music generation" function. Users can use text to describe the music they want, and AI will generate music clips within 60 seconds to facilitate advertising. and film music

Win10 player progress bar does not move Win10 player progress bar does not move Feb 12, 2024 am 08:12 AM

The October update version of Windows 10v1809 is heading towards the worst Windows upgrade in history without hesitation. Not only was it urgently withdrawn after its first official release, but it was still full of bugs after being rebuilt for a month, making people doubt Microsoft's quality control. Getting more and more worried. Now, it has one more bug on its list, and this time it’s Microsoft’s own media player, Windows Media Player. Recently, some netizens have reported that after installing the latest patch, Windows Media Player in Windows 10v1809 has an issue where the playback progress bar cannot be dragged. No solution has been found yet. Microsoft has confirmed a bug involving two patches for KB4

Choosing the right tablet for music students Choosing the right tablet for music students Jan 10, 2024 pm 10:09 PM

Which tablet is suitable for musicians? The 12.9-inch speaker in Huawei’s iPad is a very good product. It comes with four speakers and the sound is excellent. Moreover, it belongs to the pro series, which is slightly better than other styles. Overall, ipad pro is a very good product. The speaker of this mini4 mobile phone is small and the effect is average. It cannot be used to play music externally, you still need to rely on headphones to enjoy music. Headphones with good sound quality will have a slightly better effect, but cheap headphones worth thirty or forty yuan cannot meet the requirements. What tablet should I use for electronic piano music? If you want to buy an iPad larger than 10 inches, I recommend using two applications, namely Henle and Piascore. Provided by Henle

How to play music on WeChat How to play music on WeChat Feb 23, 2024 pm 09:28 PM

How to play music on WeChat? You can play your favorite music on WeChat APP, but most friends don’t know how to play their favorite music on WeChat. Next is the graphic tutorial on how to play music on WeChat brought by the editor. Interested users come and take a look! WeChat usage tutorial: How to play music on WeChat 1. First open the WeChat APP, slide down from the top to enter the mini program page; 2. Then click [Music] as shown by the arrow in the picture below; 3. Then in the interface as shown below, enter the search box Enter your favorite song title; 4. Finally, select the corresponding song title and click to play the song.

How to add local music to soda music How to add local music to soda music Feb 23, 2024 pm 07:13 PM

How to add local music to Soda Music? You can add your favorite local music to Soda Music APP, but most friends don’t know how to add local music. Next is the graphic tutorial on how to add local music to Soda Music brought by the editor. , interested users come and take a look! Tutorial on using soda music. How to add local music to soda music. 1. First open the soda music APP and click on the [Music] function area at the bottom of the main page; 2. Then enter the play page and click the [three dots] icon in the lower right corner; 3. Finally Expand the function bar below and select the [Download] button to add it to local music.

How to solve the problem that Win10's built-in player HEVC video extension requires payment? How to solve the problem that Win10's built-in player HEVC video extension requires payment? Feb 09, 2024 pm 02:54 PM

As the usage of computers becomes higher and higher, you may sometimes encounter the situation where Win10 system charges for playing HEVC videos. How should you deal with this situation? Let’s take a look at the details with the editor below. Currently, more and more videos are encoded using HEVC, which is especially common in 4K videos. In order to improve image quality and reduce size, 1080p videos are also heavily encoded with HEVC. Win10 video player does not support HEVC by default, which still affects its use. The lack of HEVC encoding support not only affects video playback, but also affects the opening of pictures. We know that many new mobile phones such as iPhone use the HEIF format to save pictures. In fact, HEIF pictures can be regarded as HEVC video encoded pictures.

See all articles