当前位置: 首页 > 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>
http://www.dtcms.com/a/243154.html

相关文章:

  • # 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打印远程控制一键部署过程!
  • 牛市与熊市:市场周期的双面镜
  • 记录下three.js学习过程中不理解问题----材质(material)⑤
  • NLP学习路线图(三十九):对话系统
  • 2025年渗透测试面试题总结-长亭科技[实习]安全服务工程师题目+回答)
  • 如何将文件从 iPhone 传输到闪存驱动器
  • GaussDB分布式数据库调优方法总结:从架构到实践的全链路优化指南
  • 增强现实—Flame: Learning to navigate with multimodal llm in urban environments
  • 安全生产管理是什么?安全生产管理主要管什么?
  • NLP学习路线图(四十):文本与图像结合
  • 增强现实—Where am I? Cross-View Geo-localization with Natural Language Descriptions
  • 腾讯云TVP走进泸州老窖,解码AI数智未来