当前位置:网站首页>UNI-APP_ Playback and pause of background music of applet or H5 page
UNI-APP_ Playback and pause of background music of applet or H5 page
2022-07-24 07:27:00 【Small skew | front end】
Online audio mp3 Address of the test
http://music.163.com/song/media/outer/url?id=447925558.mp3
stay uni-app Write small programs or h5 When the page is , Use background music , And the rotation animation of the icon
One 、 establish music.js Put it in static Or create a new folder
const bgm = uni.createInnerAudioContext();
bgm.src = '../static/bgMusic.mp3'
bgm.loop = true; // Loop Playback
// bgm.autoplay=true; // Auto play
bgm.sessionCategory = 'soloAmbient'; // Do not play in the background , If there is any other play , Will pause other playback ( But on the mobile side h5 in Failure of background not playing )( Other properties are self Baidu )
bgm.volume = 0.4 // The volume
var music = {
//mute Indicates whether it is mute ,, Default mute
playBgm({
mute = true }) {
if (!bgm) return;
if (mute) {
bgm.pause()
} else {
bgm.play()
}
bgm.onPause(() => {
})
bgm.onPlay(() => {
})
bgm.onError((res) => {
})
}
}
module.exports = music
Two 、 Mount to vue In the example , stay main.js in
import music from './base/music.js'
Vue.prototype.$music = music
3、 ... and 、 Put it on the required page
Dynamically add rotation styles
<template>
<view>
<img src="../../static/music.png" alt="" :class="[muteBgMusic ? '' : 'music']" @click="playMusic"/>
</view>
</template>
export default {
data() {
return {
muteBgMusic: true, // Mute
}
},
watch: {
muteBgMusic(newValue, oldValue) {
if (newValue) {
// Turn on mute
this.$music.playBgm({ mute: true });
} else {
// close Mute
this.$music.playBgm({ mute: false });
}
}
},
methods: {
// Click whether to mute because ios Can't auto play , So I unify Android iOS Both ends need to manually click to start playing
// If you want to open the page, play , Just in music.js in Change the default value to false, And set automatic playback
playMusic() {
this.muteBgMusic = !this.muteBgMusic;
}
}
};
</script>
<style lang="scss" scoped> .music {
animation: music 2.5s linear 0s infinite normal none; } @keyframes music {
0% {
transform: rotate(0deg); } 10% {
transform: rotate(36deg); } 20% {
transform: rotate(72deg); } 30% {
transform: rotate(108deg); } 40% {
transform: rotate(144deg); } 50% {
transform: rotate(180deg); } 60% {
transform: rotate(216deg); } 70% {
transform: rotate(252deg); } 80% {
transform: rotate(288deg); } 90% {
transform: rotate(324deg); } 100% {
transform: rotate(360deg); } } </style>
边栏推荐
- Blockbuster live broadcast | orb-slam3 series code explanation map points (topic 2)
- [PTA] group programming ladder competition - Summary of exercises L3 (incomplete)
- 【PTA】团体程序设计天梯赛-练习集 L3题目总结(不全)
- Wild pointer, null pointer, invalid pointer
- File "manage.py", line 14) from exc ^ syntaxerror: cause and solution of invalid syntax error
- [leetcode simple] 20. Valid brackets stack
- Paper reading: hardnet: a low memory traffic network
- Variables and data types (03)
- 【时序逻辑电路】——寄存器
- Customization or GM, what is the future development trend of SaaS in China?
猜你喜欢

SPI - send 16 bit and 8-bit data

PHP link log scheme

Who can stand it when the project goes online

Vulnhub DC1

拉普拉斯(Laplace)分布

AMD64(x86_64)架构abi文档:上

Variables and data types (04) end

Decompress the anchor and enjoy 4000w+ playback, adding a new wind to the Kwai food track?

AMD64 (x86_64) architecture ABI document: upper

Unity中使用深度和法线纹理
随机推荐
我的创作纪念日
Aggregated new ecological model - sharing purchase, membership and reward system
Part II - C language improvement_ 4. Secondary pointer
numpy.concatenate
【云原生】MySql索引分析及查询优化
FPGA realizes reading and writing of axi4 bus
单场GMV翻了100倍,冷门品牌崛起背后的“通用法则”是什么?
Jenkins 详细部署
野指针,空指针,失效指针
File upload and download demo
R language handwritten numeral recognition
php 转义字符串
Chinese manufacturers may catch up with the humanoid robot Optimus "Optimus Prime", which has been highly hyped by musk
JS_实现多行文本根据换行分隔成数组
Using depth and normal textures in unity
Blockbuster live broadcast | orb-slam3 series code explanation map points (topic 2)
2D目标检测综述之学习策略篇(终章)
JMeter notes 2 | JMeter principle and test plan elements
Buddy: core function entry
论文阅读:HarDNet: A Low Memory Traffic Network