uni-app中的createInnerAudioContext使用

该博客详细介绍了如何在uni-app中实现音频读题的功能。通过创建InnerAudioContext对象,设置音频属性并监听播放状态,实现了音频的播放、暂停和继续播放。同时,利用Baidu的TTS服务生成文本到语音的URL,实现了读题文字的语音合成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
	<view>
		<!-- 读题 -->
		<image class="read_img" @tap="read_btn" src="" mode="widthFix"></image>
	</view>
</template>
<script>
	var innerAudioContext = uni.createInnerAudioContext();
	innerAudioContext.autoplay = false;	//不自动播放
	innerAudioContext.src = '';
	export default {
		data() {
			return {
				read_status: false, //读题状态
				read_url: ''
			}
		},
		methods: {
			//读题
			read_btn() {
				var _this = this;
				if(this.read_status == false) {
					this.play_audio()
				}else{
					this.stop_read()
				}

			},
			stop_read() {
				var _this = this;
				innerAudioContext.pause()
				 innerAudioContext.onPause(function() {
					console.log('暂停播放');
					_this.currentTime = innerAudioContext.currentTime;
				  })
				   this.read_status = false;
			},
			play_audio() {
				var _this = this;
				var token = _this.current_timu.token;
				var Text = _this.current_timu.text;
				
				this.read_url = `http://tsn.baidu.com/text2audio?per=0&ctp=1&lan=zh&ie=UTF-8&spd=4&vol=5%pit=5&tok=${token}&cuid=123456PHP&tex='${Text }'`;
				innerAudioContext.src = this.read_url;
				innerAudioContext.play(); //直接播放
				innerAudioContext.onPlay(function() { //暂停之后点击播放继续播放
					uni.showToast({
						title: '播放中...',
						icon: 'none'
					})
					if (_this.currentTime) {
						innerAudioContext.seek(_this.currentTime)
					}
				})
				this.read_status = true;
			}
		}
	}
</script>

uni中的audio介绍文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值