当前位置: 首页 > news >正文

uniapp音乐播放createInnerAudioContext

利用uniapp的api createInnerAudioContext音乐播放

1.html部分

<template><view style="padding: 30vh 20% 0 20%;" class="flex-space-center"><view @click="handleDo('stop')" v-if="status != 'stop'">结束</view><view @click="handleDo('play')" v-if="status =='stop'">开始</view><view @click="handleDo('pause')" v-if="status == 'play' || status =='resum'">暂停</view><view @click="handleDo('resum')" v-if="status == 'pause'">继续</view><view :class="{play:isPlay,pause:isPause}"style="display: inline-block;height: 40rpx;width: 40rpx;background: red;" @click="handleImg">5</view></view>
</template>

2.script部分

<script>const innerAudioContext = uni.createInnerAudioContext();export default {components: {},data() {return {isPause: false,isPlay: false,status: 'stop', //stop 结束  play播放  pause暂停  resum继续bgmusic: '',src_bgmusic: 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3'}},onLoad(options) {this.init(options)},onHide() {//注意:uni.navigateBack到下一个页面时,音乐会继续播放,所以我们要在这里设置一下暂停if (this.bgmusic) {this.voice_pause()}},onShow() {// 从下一个返回到这个页面时继续if (this.bgmusic) {this.audioResum()}},onUnload() {// 页面销毁时暂停音乐this.voice_stop()},onReady() {},methods: {init(options) {// 自然播放结束事件innerAudioContext.onEnded(() => {this.audioPaly()})// 模拟接口请求背景音乐---setTimeout(() => {this.bgmusic = this.src_bgmusic;}, 0)},// 播放audioPaly() {this.isPause = falsethis.isPlay = trueinnerAudioContext.src = this.bgmusicinnerAudioContext.play()this.status = 'play'},// 继续audioResum() {this.isPause = falsethis.isPlay = trueinnerAudioContext.play()this.status = 'resum'},// 暂停audioPause() {this.isPause = true;this.isPlay = false;innerAudioContext.pause()this.status = 'pause'},// 结束audioStop() {this.isPause = truethis.isPlay = false;innerAudioContext.stop()this.status = 'stop'},// 操作handleDo(status) {switch (status) {case 'stop':this.audioStop()break;case 'play':this.audioPaly()break;case 'pause':this.audioPause()break;case 'resum':this.audioResum()break;}},// 点击旋转handleImg() {switch (this.status) {case 'play':this.audioPause()break;case 'pause':this.audioResum()break;case 'resum':this.audioPause()break;case 'stop':this.audioPaly()break;}}}}
</script>

style部分

<style lang="scss" scoped>.play {animation: rotate 3s linear infinite;}.pause {animation-play-state: paused;}@keyframes rotate {to {transform: rotate(360deg);}}
</style>

相关文章:

  • # include<heαd.h>和# include″heαd.h″的区别
  • Argocd 运维与最佳实践
  • 【uniapp】小程序中input输入框的placeholder-class不生效
  • 【C++】来学习使用set和map吧
  • matlab分布式电源微电网潮流
  • DAS-U250高性能分布式光纤声波传感器
  • Spring @Scope, @Lazy, @DependsOn, @Required, @Lookup
  • 机器学习核心概念速览
  • Springboot项目的目录结构
  • AI赋能Automa二次开发
  • Spring @Qualifier,@Primary
  • 分布式ID实现方案实战示例总结
  • 【Zephyr 系列 18】分布式传感网络系统设计:从 BLE Mesh 到边缘网关的数据闭环
  • 看安科瑞分布式光伏解决方案如何破解光伏痛点?
  • 机器学习赋能的智能光子学器件系统研究与应用
  • 基于生成对抗网络(GAN)的图像生成与编辑:原理、应用与实践
  • Idea 2025 commit 关闭侧边栏 开启探框
  • SHA-1算法详解:原理、特点与应用
  • OctoPrint公网部署如何实现?3D打印远程控制一键部署过程!
  • 牛市与熊市:市场周期的双面镜
  • seo1网站查询/网络营销概述
  • 做网站平面模板是啥意思/seo这个职位是干什么的
  • 网站死链接查询/搜索引擎优化趋势
  • 有哪些可以做网站的平台/互联网营销师是什么
  • 广州知名网站建设性价比高/seo搜索引擎优化推荐
  • 广州网站设计/百度网页pc版登录