uniapp音乐播放createInnerAudioContext

利用uniapp的api createInnerAudioContext音乐播放

1.html部分

javascript 复制代码
<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部分

javascript 复制代码
<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 = false
				this.isPlay = true
				innerAudioContext.src = this.bgmusic
				innerAudioContext.play()
				this.status = 'play'
			},
			// 继续
			audioResum() {
				this.isPause = false
				this.isPlay = true
				innerAudioContext.play()
				this.status = 'resum'
			},
			// 暂停
			audioPause() {
				this.isPause = true;
				this.isPlay = false;
				innerAudioContext.pause()
				this.status = 'pause'
			},
			// 结束
			audioStop() {
				this.isPause = true
				this.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部分

javascript 复制代码
<style lang="scss" scoped>
	.play {
		animation: rotate 3s linear infinite;
	}

	.pause {
		animation-play-state: paused;
	}

	@keyframes rotate {
		to {
			transform: rotate(360deg);
		}
	}
</style>
相关推荐
万物得其道者成10 小时前
UniApp 多端滑块验证码插件 zxj-slide-verify 实用指南
uni-app
蓝帆傲亦12 小时前
支付宝小程序性能暴增秘籍:UniApp项目极限优化全攻略
小程序·uni-app
2501_9160088921 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
QT.qtqtqtqtqt1 天前
uni-app小程序前端开发笔记(更新中)
前端·笔记·小程序·uni-app
喵喵虫1 天前
uniapp修改封装组件失败 styleIsolation
uni-app
游戏开发爱好者82 天前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 天前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 天前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息2 天前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”2 天前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别