微信小程序自定义音频组件,自定义滚动条,单曲循环,循环播放 小程序自定义音频组件,带滚动条

效果如图

1,app,json里面配置

 "requiredBackgroundModes": [
    "audio",
    "location"
  ],

2,app.js里面配置

globalData: {
    userInfo: null,
    global_bac_audio_manager: {
      manage: wx.getBackgroundAudioManager(),
      is_play: false,
      id: '',
      play_time: '',
      article_id: '',
    }
  }

3,index.js里面的代码

const APP = getApp()
const AUDIOMANAGER = getApp().globalData.global_bac_audio_manager.manage
const AUDIO = getApp().globalData.global_bac_audio_manager


Page({
  /**
   * 页面的初始数据
   */
  data: {
    is_play: 1,//是否播放音乐
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      preArticleId: 0,//是否存在上一首
      nextArticleId: 1,//是否存在下一首
      is_loop: true,//是否循环播放
      is_play: true,//是否正在播放
    })
    AUDIOMANAGER.onPlay(() => {
      setTimeout(() => {
        that.setData({
          is_loading: true
        })
      }, 300)
    })
    let response = {
      preArticleId: 0,
      nextArticleId: 1,
      urlCompressed: 'http://www.wgx0725.com/music1.mp3',
      articleName: '测试音乐',
      lessonName: '专辑名',
      poster: 'http://www.1810.com.cn/static/uploadfile/file/2019-02-19/5c6bd0b57f8dc.jpg',
    }

    // 如果不是从悬浮按钮播放,就重新赋值
    if (options.articleId == AUDIO.id && AUDIO.is_play) {
      wx.seekBackgroundAudio({
        position: Math.floor(AUDIO.time)
      })
    } else {
      audio_background_play(response)
    }

    // 置灰上一首下一首
    if (response.preArticleId == 0) {
      that.setData({
        is_first_page: true
      })
    }
    if (response.nextArticleId == 0) {
      that.setData({
        is_last_page: true
      })
    }
    //背景音频播放进度更新事件
    AUDIOMANAGER.onTimeUpdate(() => {
      if (!that.data.is_moving_slider) {
        that.setData({
          current_process: format(AUDIOMANAGER.currentTime),
          slider_value: Math.floor(AUDIOMANAGER.currentTime),
          total_process: format(AUDIOMANAGER.duration),
          slider_max: Math.floor(AUDIOMANAGER.duration)
        })
      }
      AUDIO.time = AUDIOMANAGER.currentTime
    })

    // 背景音频播放完毕
    AUDIOMANAGER.onEnded(() => {
      if (that.data.is_loop) {
        that.next()
      } else {
        // 单曲循环
        that.setData({
          slider_value: 0,
          current_process: '00:00',
        })
        audio_background_play(response)
      }
    })
  },
 

  // 拖动进度条,到指定位置
  hanle_slider_change(e) {
    const position = e.detail.value
    this.seekCurrentAudio(position)
  },
  // 拖动进度条控件
  seekCurrentAudio(position) {
    // 更新进度条
    let that = this
    wx.seekBackgroundAudio({
      position: Math.floor(position),
      success: function () {
        AUDIOMANAGER.currentTime = position
        that.setData({
          current_process: format(position),
          slider_value: Math.floor(position)
        })
      }
    })
  },
  // 进度条滑动
  handle_slider_move_start() {
    this.setData({
      is_moving_slider: true
    });
  },
  handle_slider_move_end() {
    this.setData({
      is_moving_slider: false
    });
  },
  // 点击播放暂停
  audio_play: function () {
    let that = this
    if (this.data.is_play) {
      that.setData({
        is_play: false
      })
      wx.pauseBackgroundAudio()
    } else if (!this.data.is_play && this.data.is_ended) { // 这里是判断如果循环播放结束,没有下一首,重新播放 is_ended  是否是最后一首
      audio_background_play(that.data.audio_article)
      that.setData({
        is_play: true,
        is_ended: false
      })
    } else if (!this.data.is_play) {
      that.setData({
        is_play: true
      })
      wx.playBackgroundAudio()
    }
    AUDIO.is_play = !AUDIO.is_play
  },
  // 点击是否循环
  play_loop: function () {
    let that = this
    if (this.data.is_loop) {
      that.setData({
        is_loop: false
      })
    } else {
      that.setData({
        is_loop: true
      })
    }
  },
  // 上一首
  prev: function () {
    let that = this
    if (that.data.preArticleId != 0) {
      //存在上一首,那么就更改当前的播放id,并且重新获取当前id是否存在上一首和下一首
      wx.redirectTo({
        url: '/pages/audio_article/audio_article?articleId=' +
          that.data.audio_article.preArticleId
      })
    }
  },
  // 下一首
  next: function () {
    let that = this
    if (that.data.nextArticleId != 0) {
      let response = {
        preArticleId: 0,
        nextArticleId: 1,
        urlCompressed: 'http://www.wgx0725.com/music2.mp3',
        articleName: '测试音乐2',
        lessonName: '专辑名2',
        poster: 'http://www.1810.com.cn/static/uploadfile/file/2019-02-19/5c6bd0b57f8dc.jpg',
      }
      // 单曲循环
      that.setData({
        slider_value: 0,
        current_process: '00:00',
      })
      audio_background_play(response)

    } else { // 如果是最后一首
      that.setData({
        is_play: false,
        slider_value: 0,
        current_process: '00:00',
        is_ended: true
      })
      AUDIO.is_play = false
    }
  },
  onUnload: function () {
    // 动态切换悬浮按钮的动态
    if (AUDIO.is_play) {
      APP.globalData.is_active = true
    } else {
      APP.globalData.is_active = false
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  }
})

// 时间格式化
function format(t) {
  let time = Math.floor(t / 60) >= 10 ? Math.floor(t / 60) : '0' + Math.floor(t / 60)

  t = time + ':' + ((t % 60) / 100).toFixed(2).slice(-2)
  return t
}
// 音频播放
function audio_background_play(response) {
  AUDIOMANAGER.src = response.urlCompressed ? response.urlCompressed : response.audioLink // 音频的数据源,默认为空字符串,当设置了新的 src 时,会自动开始播放 ,目前支持的格式有 m4a, aac, mp3, wav
  AUDIOMANAGER.title = response.articleName // 音频标题
  AUDIOMANAGER.epname = response.lessonName // 专辑名
  AUDIOMANAGER.singer = '****' // 歌手名
  AUDIOMANAGER.coverImgUrl = response.poster // 封面图url
}

4,index.wxml 代码

<!--音频播放html-->
 <view class="audio">
    <image class="bg" src="../../images/background.png"></image>
   <text class='txt'>测试音乐</text>
    <view class="control-process">
        <text class="current-process">{{current_process}}</text>
        <slider class="slider" 
            bindchange="hanle_slider_change" 
            bindtouchstart="handle_slider_move_start" 
            bindtouchend="handle_slider_move_end" 
            min="0" 
            block-size="16" 
            max="{{slider_max}}" 
            activeColor="#fff" 
            backgroundColor="rgba(255,255,255,.3)" 
            value="{{slider_value}}"
        />
        <text class="total-process">{{total_process}}</text>
    </view>
    <view class="icon-list ">
        <image bindtap="prev" mode="widthFix" src="{{is_first_page?'../../images/audio_prev_no.png':'../../images/audio_prev.png'}}" class="audio-icon"></image>
        <image mode="widthFix" src="{{is_play? '../../images/audio_play.png': '../../images/audio_paused.png'}}" class="audio-icon audio-play" bindtap="audio_play"></image>
        <image bindtap="next" mode="widthFix" src="{{is_last_page?'../../images/audio_next_no.png':'../../images/audio_next.png'}}" class="audio-icon"></image>
        <image mode="widthFix" class="launch" src="{{is_loop ? '../../images/audio_loop.png': '../../images/audio_un_loop.png'}}" bindtap="play_loop"></image>
    </view>
</view>

5,index.wxss代码

/* pages/detail/detail.wxss */
page {
  background-color: rgb(218, 226, 230);
}


.audio {
    position:fixed;
    width:750rpx;
    height:185rpx;
    padding:2rpx 32rpx 5rpx;
    box-sizing:border-box;
    text-align:center;
    overflow:hidden;
    background:rgba(88, 199, 233, 0.89);
    bottom:0px;
    z-index: 999;
}
.audio .bg {
    position: absolute;
    top: 0;
    left: -100%;
    bottom: 0;
    right: 0;
    margin: auto;
    width: 300%;
    height: 300%;
    z-index: -1;
    filter: blur(40rpx);    
}
.audio .txt{
  height:20px;
display:block;
line-height:20px;
font-size:12px;
color:#ddd;
padding-bottom:-20px;
margin-bottom:-20px;

}
.editor {
    padding: 32rpx;
    box-sizing: border-box;
    color: #333;
    font-size: 28rpx;
    background: #fff;
}
.editor view {
    max-width: 100% !important;
}

.audio .poster {
    width: 238rpx;
    height: 336rpx;
}
/* 音频滚动条start */
.control-process {
    margin-top: 20rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.control-process .slider {
    width: 526rpx;
}
.control-process text {
    font-size: 24rpx;
    color: #fff;
}
/* 音频滚动条end */
.audio .icon-list {
    position: relative;
    margin: 0 auto;
    line-height: 66rpx;
}

.audio .icon-list .audio-icon + .audio-icon {
    margin-left: 72rpx;
}

.audio .icon-list .pattern {
    position: absolute;
    right: 20rpx;
}

.audio image {
    width: 50rpx;
    height: 50rpx;
    vertical-align: middle;
}
.audio .launch{position:absolute;
margin-left:20px;
margin-top:5px;
}
.audio .audio-play {
    width: 50rpx;
    height: 50rpx;
}

.audio .pattern {
    position: absolute;
    top: 0;
    bottom: 0;
    margin: auto 0;
    width: 44rpx;
    height: 44rpx;
}
  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
微信小程序是一种基于微信平台的应用程序,它可以在微信中直接运行,无需下载安装。而自定义组件小程序中的一种重要功能,它允许开发者将一些常用的UI元素封装成组件,以便在不同的页面中复用。 自定义组件具有以下特点: 1. 组件是由wxml、wxss和js文件组成,可以独立定义样式和逻辑。 2. 组件可以接受外部传入的数据,通过属性进行配置。 3. 组件可以触发事件,向外部传递消息。 4. 组件可以包含子组件,形成组件的嵌套结构。 使用自定义组件的步骤如下: 1. 在小程序项目中创建一个新的文件夹,用于存放自定义组件的相关文件。 2. 在该文件夹中创建一个wxml文件,定义组件的结构。 3. 在同一文件夹中创建一个wxss文件,定义组件的样式。 4. 在同一文件夹中创建一个js文件,定义组件的逻辑。 5. 在需要使用该组件的页面中引入组件,并在wxml中使用组件标签。 例如,我们创建一个名为"custom-component"的自定义组件,其文件结构如下: ``` custom-component/ ├── custom-component.wxml ├── custom-component.wxss └── custom-component.js ``` 在custom-component.wxml中定义组件的结构,例如: ```html <view class="custom-component"> <text>{{text}}</text> <button bindtap="handleClick">点击按钮</button> </view> ``` 在custom-component.wxss中定义组件的样式,例如: ```css .custom-component { background-color: #f5f5f5; padding: 10px; } ``` 在custom-component.js中定义组件的逻辑,例如: ```javascript Component({ properties: { text: { type: String, value: '默认文本' } }, methods: { handleClick() { this.triggerEvent('click', { message: '按钮被点击了' }); } } }) ``` 在需要使用该组件的页面中引入组件,并在wxml中使用组件标签,例如: ```html <custom-component text="Hello World" bind:click="handleCustomComponentClick"></custom-component> ``` 以上就是微信小程序定义组件的简单介绍。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值