<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-app中的createInnerAudioContext使用
最新推荐文章于 2024-11-03 18:46:27 发布
该博客详细介绍了如何在uni-app中实现音频读题的功能。通过创建InnerAudioContext对象,设置音频属性并监听播放状态,实现了音频的播放、暂停和继续播放。同时,利用Baidu的TTS服务生成文本到语音的URL,实现了读题文字的语音合成。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
HunyuanVideo-Foley
语音合成
HunyuanVideo-Foley是由腾讯混元2025年8月28日宣布开源端到端视频音效生成模型,用户只需输入视频和文字,就能为视频匹配电影级音效

1万+

被折叠的 条评论
为什么被折叠?



