Home WeChat Applet Mini Program Development How to create a music playback bar in a small program

How to create a music playback bar in a small program

Mar 19, 2020 pm 06:06 PM
Applets music

How to create a music playback bar in a small program

How to make a music playback bar in a small program

You can use the progress component to implement a music playback bar. The specific method is as follows:

1. Add an audio tag and hide it without specifying the controls="true" attribute.

 <view class="audio-play">
    <audio src=""></audio>
  </view>
  
  <view>
    <view class="one-column play-it" bindtap="playMusic">
      <view>点击播放</view>
    </view>
    
    <progress class="music-prog" bindtouchmove="setTouchMove" percent="{{musicPercent}}"></progress>
    
    <view class="percent-num">{{musicPercent}}%</view>
  </view>
Copy after login

bindtouchmove represents a touch event;

The progress tag sets the progress through the percent attribute

2. Write WSS files

Recommended learning :小program development

.play-it{
  margin-left: 300rpx;
}

.music-prog{
  width: 550rpx;
  height: 10rpx;
  margin: 50rpx 100rpx;
  color: #0099ff;
  background-color: #999;
}

.percent-num{
  margin: -20rpx 0 0 100rpx;
  font-size: 28rpx;
}
Copy after login

3. Write js to control the playback bar.

onShow() {
    // 监听音乐播放
    let that = this
    wx.onBackgroundAudioPlay(() => {
      that.timer && clearInterval(that.timer)
      that.timer = setInterval(() => {
        wx.getBackgroundAudioPlayerState({
          success: res => {
            let per = (res.currentPosition/res.duration)*10000
            that.setData({
              musicPercent: Math.round(per)/100 + &#39;&#39;,
              duration: res.duration
            })
          }
        })
      }, 1000)
    })

    // 监听背景音频暂停事件
    wx.onBackgroundAudioPause(() => {
      clearInterval(that.timer)
    })

    // 监听背景音频停止事件
    wx.onBackgroundAudioStop(() => {
      clearInterval(that.timer)
    })
  },
  
  playMusic() {
 	 let obj = {
       dataUrl: &#39;http://p6jceeddp.bkt.clouddn.com/%E5%B0%A4%E9%95%BF%E9%9D%96%20-%20%E6%98%A8%E6%97%A5%E9%9D%92%E7%A9%BA.mp3&#39;,
        title: &#39;昨日青空&#39;,
        coverImgUrl: &#39;/static/images/avatar.png&#39;
      }
      wx.playBackgroundAudio(obj)
    },
 
  setTouchMove (e) {
    if(e.touches[0].clientY >= 390 && e.touches[0].clientY <= 410) {
      if (e.touches[0].clientX >= 55 && e.touches[0].clientX <= 355) {
        let percent = (e.touches[0].clientX - 55)/300*10000
        this.setData({
          musicPercent: Math.round(percent)/100 + &#39;&#39;
        })
        this.data.current = (this.data.musicPercent/100)*this.data.duration
      }
    }
  },
  
  setProgress() {
    let that = this
    console.log(&#39;bindtouchend&#39;)
    wx.getBackgroundAudioPlayerState({
      success: res => {
        that.data.current !== res.currentPosition &&
        wx.seekBackgroundAudio({
          position: that.data.current,
          success () {
            console.log(&#39;seek&#39;, that.data.current)
          }
        })
      }
    })
  }
Copy after login

The effective area of ​​the playback bar

Horizontal: e.touches[0].clientX

Vertical: e.touches[0].clientY

The horizontal range here is 55~355 and the vertical range is 390~410

Define the touch event

The obtained horizontal progress bar position , calculate the position of the progress bar dragged by the user

**Note: Calling wx.seekBackgroundAudio() here to set the playback progress will cause the audio to freeze. Because the seek method will be called multiple times during the dragging process, the playback progress setting should be executed after the dragging progress bar is completed.

touchend monitors the stop of touch events

Calculate wx.seekBackgroundAudio() to set the playback progress based on the time current calculated in the touch event

Effect :

How to create a music playback bar in a small program

PHP Chinese website, a large number of website construction tutorials, welcome to learn!

The above is the detailed content of How to create a music playback bar in a small program. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

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.

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

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

Implement card flipping effects in WeChat mini programs Implement card flipping effects in WeChat mini programs Nov 21, 2023 am 10:55 AM

Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: &lt;!--index.wxml--&gt;&l

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 fix Apple Music songs not available in your region How to fix Apple Music songs not available in your region Jul 17, 2023 pm 08:24 PM

Why does my Apple Music keep saying music is not available? Before proceeding with the remedies, it is crucial to understand why AppleMusic shows notification Music Not Available. There are several reasons for this issue: Location restrictions – Some tracks or albums may be blocked in your region due to licensing agreements or copyright issues. Music accessibility varies by region, and your specific content may not have a distribution license. Regional Release Dates – Music releases are often spaced out across many countries. Certain songs or albums may already be circulating in a particular country but are still awaiting release in another country. Subscription Limitations – If you have an Apple Music membership, the conditions of your subscription plan may affect your access to specific songs or albums.

Detailed steps to view your favorite music on Douyin Detailed steps to view your favorite music on Douyin Mar 26, 2024 pm 06:20 PM

1. Click [+]. 2. Click [Select Music] above. 3. Click [My Favorites]. Method 2: 1. Open Douyin and click [Me]. 2. Click [Collect] next to the avatar. 3. Click [Music]

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.

See all articles