微信小程序音频背景播放
由于微信小程序官方将音频的样式固定死了,往往再工作中和UI设计师设计出来的样式不符,故一般都采用背景音频播放来实现自定义的UI样式的音频播放,即使用官网API提供的BackgroundAudioManager进行操作 也可以去官网https://developers.weixin.qq.com/miniprogram/dev/api/media/background-audio/BackgroundAudioManager.play.html查看详情
第一步,在对应的wxml页面添加如下代码,eg,放在index.wxml中
<view class='audioMeaage'>
<view bindtap='playAudio1'>
<image wx:if="{{!audioImg}}" class='playIcom' src='{{palyIcon}}'></image>
<image wx:else class='playIcom' src='{{pausIcon}}'></image>
</view>
<view class='sliderWrap'>
<view class='audioNames'>{{audioTitle}}</view>
<view class='progressWrap'>
<slider class="slider2"
bindchange="hanle_slider_change"
bindtouchstart="handle_slider_move_start"
bindtouchend="handle_slider_move_end"
min="0"
block-size="10"
max="{{slider_max}}"
activeColor="#ffd038"
block-color="#ffd038"
backgroundColor="#f2f2f2"
value="{{slider_value}}"
/>
<view class='proTime'>{{current_process}}/{{total_process}}</view>
</view>
</view>
</view>
第二步,对应的index.js,注意这里的playAudio方法是点击以后从后台获取音频的url和标题等信息的
//获取应用实例
const app = getApp()
const AUDIOMANAGER = getApp().globalData.global_bac_audio_manager.manage
const AUDIO = getApp().globalData.global_bac_audio_manager
Page({
data: {
audioImg:false,
palyIcon: "./../../images/home_img_vedio_play.png",
pausIcon:"./../../images/home_img_vedio_play2.png",
audioFlag: false,
is_moving_slider: false,
current_process:"",
slider_value: "",
total_process: "",
slider_max: "",
audioTitle:"",
},
playAudio: function(e){
const item = e.currentTarget.dataset.url
AUDIOMANAGER.src = item.link_url
AUDIOMANAGER.title = item.tit // 音频标题 AUDIO.is_play= true
//背景音频播放进度更新事件
const that = this
that.setData({
audioFlag: true,
audioTitle: item.tit,
audioImg: true
})
AUDIOMANAGER.onTimeUpdate(() => {
if (!that.data.is_moving_slider) {
that.setData({
current_process: that.format(AUDIOMANAGER.currentTime),
slider_value: Math.floor(AUDIOMANAGER.currentTime),
total_process: that.format(AUDIOMANAGER.duration),
slider_max: Math.floor(AUDIOMANAGER.duration)
})
}
AUDIO.time = AUDIOMANAGER.currentTime
}) // 背景音频播放完毕
AUDIOMANAGER.onEnded(() => {
// 单曲循环
that.setData({
slider_value: 0,
current_process: '00:00',
total_process: that.format(AUDIOMANAGER.duration)
})
})
},
// 拖动进度条,到指定位置
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: that.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
});
},
// 时间格式化
format: function(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
},
// 播放音频
playAudio1: function(){
console.log(9799875)
console.log(AUDIO.is_play)
if(AUDIO.is_play) {
AUDIOMANAGER.pause()
AUDIO.is_play = false
this.setData({
audioImg: false
})
} else {
AUDIOMANAGER.play()
AUDIO.is_play = true
this.setData({
audioImg: true
})
} },
})
第三步,由于index.js里引入了app.js的方法或者属性了,其实就是将AUDIO的方法提出来了,放在了一个公用的里面,自己单独建立公用文件或者直接写在app.js里都是可以的,这里将其放在app.js中,可以对应这修改globalData里的属性
globalData: {
userInfo: null,
global_bac_audio_manager: {
manage: wx.getBackgroundAudioManager(),
is_play: false,
id: '',
play_time: '',
article_id: '',
}
}
index.wxss
.audioMeaage{
height: 124rpx;
display: flex;
align-items: center;
}
.proTime{
font-size: 20rpx;
color: #888888;
}
.progressWrap{
display: flex;
align-items: center;
}
.playIcom{
width: 84rpx;
height: 84rpx;
}
.slider{
/* width: 502rpx; */
}
.slider1{
width: 466rpx;
margin:0 29rpx 0 8rpx;
}
.slider2{
width: 466rpx;
margin:0 29rpx 0 19rpx;
}
微信小程序音频背景播放的更多相关文章
- 如何使用微信小程序video组件播放视频
相信很多人都有在手机上看视频的习惯,比较看视频更真实更形象.那么我们在微信小程序中如何观看视频呢?这就需要video组件的帮忙了.今天我们就给大家演示一下,如何用微信小程序组件video播放视频.我们 ...
- 微信小程序音频播放 InnerAudioContext 的用法
今天项目上涉及到了微信小程序播放音频功能,所以今天跟着一些教程做了个简单的播放器 1.实现思路 刚开始想着有没有现成的组件可以直接用,找到了微信的媒体组件 audio,奈何看着 1.6.0版本开始,该 ...
- 微信小程序 写音乐播放器 slider组件 将value设置为0 真机测试滑块不能回到起点
最近在用微信小程序写一个音频播放页面,做时间进度的时候用到了slider插件,但是在自然播放完成,或者上/下切换的时候,将slider的value属性值设为0,开发工具上滑块会回到起点,有效.但是真机 ...
- 微信小程序后台音乐播放注意事项
wx.seekBackgroundAudio(OBJECT) 作用:控制音乐播放进度. 注意: 该事件 会触发 wx.onBackgroundAudioPlay(CALLBACK) 事件 ,也就是相当 ...
- 微信小程序——引入背景图片【六】
前言 之前写了一些小程序的博文只是看文档边看边写,了解下他,这次可是真枪真刀的做了! 框架使用的是美团的mpvue,我也是一边学习,一边写的,如有错误之处,还望大家指出. 在这里我有个问题,为什么微信 ...
- 微信小程序开发--背景图显示
这两天开发微信小程序,在设置背景图片时,发现在wxss里面设置 background-image:(url) 属性,不管是开发工具还是线上都无法显示.经过查资料发现,background-image ...
- 微信小程序添加背景图片的坑
给微信小程序页面加载背景图片解决方案 直接附上原文地址: 给微信小程序页面加载背景图片解决方案 - YUSIR 完美CODING世界 - CSDN博客 https://blog.csdn.net/y ...
- 微信小程序——音频播放器
先来个效果图韵下味: 需求: 音频的播放,暂停,中间按钮状态的变化,播放时实时更新播放进度: 前进15s,后退15s: 进度条拖动. 一开始想着这3个功能应该挺简单的.不就是播放,暂停,前进,后退么~ ...
- 微信小程序音频长度获取的问题
小程序推荐使用wx.createInnerAudioContext()创建的innerAudioContext,我们也通过这个接口创建音频.音频的长度可以通过属性获取: 但是,给innerAudioC ...
随机推荐
- a 标签 pointer-events
a 标签渲染到也页面之后,由于某些原因需要,禁用. 有两种方法可以设置禁用效果. 1.使用 CSS 属性 pointer-events ,设置此属性为 none 之后,元素将不会成为鼠标事件的 tar ...
- VS2017 Thrift编译出的Release版本的库调用报错LNK2001
在使用thrift的过程中, 当我使用完thrift debug版本编译出来的库调试完成后, 改成release版本的时候, 就出现了如下错误, 莫名其妙啊, 同一套代码, 那只能是编译库的时候设置和 ...
- mysql8
解决navicat不能连接问题: grant all privileges on *.* to ‘root’@’%’;ALTER USER 'root'@'localhost' IDENTIFIED ...
- 1+x证书Web前端开发中级理论考试(试卷1)
2019年下半年 Web前端开发中级 理论考试 (考试时间19:00-20:30 共150分钟,测试卷1) 本试卷共3道大题,满分100分. 请在指定位置作答. 一.单选题(每小题2分,共30小题,共 ...
- 【shell脚本】通过位置变量创建Linux账户及密码===addUser.sh
通过位置变量创建Linux账户及密码 脚本内容 [root@VM_0_10_centos shellScript]# vi addUser.sh #!/bin/bash # 通过位置变量创建系统账户及 ...
- HTML+CSS基础 border css属性 Div块 盒子
border css属性 边框颜色 border-color:red/#ffffff/rgb()默认为黑色 边框样式 border-style:solid (实线) dashed (虚线).默认为n ...
- 微软宣布.NET Native预览版 C#可编译为本地机器码【转】
英文原文:Announcing .NET Native Preview 微软在 MSDN 博客上宣布了 .NET Native 的开发者预览版..NET Native 可以将 C# 代码编译成本地机器 ...
- LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination
题目 非常简单的BFS 暴搜 struct Node { int x; int y; int k; int ans; Node(){} Node(int x,int y,int k,int ans) ...
- C# Task,new Task().Start(),Task.Run();TTask.Factory.StartNew
1. Task task = new Task(() => { MultiplyMethod(a, b); }); task.Start(); 2. Task task = Task.Run(( ...
- mask-rcnn代码解读(六):resize_image()函数的解析
我已经根据resize_image()函数的解析对原图像与resize图像进行了解析, 若有读者想对原图像与目标图像不同尺寸验证,可根据以下代码,调整函数参数, 其细节如下: import cv2 a ...