uni-app/vue 文字转语音朗读(附小程序语音识别和朗读)

    语音播报的实现的方法有很多种,我这里介绍集中不引用百度、阿里或者迅飞的API的实现方式。

一、采用new SpeechSynthesisUtterance的方式

废话不多说直接上代码

data() {
	return {
		utterThis:null,
	}
},


//方法使用

this.utterThis = new SpeechSynthesisUtterance('');
this.utterThis.pitch = 1; // 音高
this.utterThis.rate = 1; // 语速
this.utterThis.volume = 1; // 音量
this.utterThis.lang = 'zh-CN';
this.utterThis.text =  "要播报的文本内容";
window.speechSynthesis.speak(this.utterThis); //开始朗读

方法的结束事件

this.utterThis.onend = () => {  //结束事件   
	window.speechSynthesis.cancel()   //注销合成方法
}

二、采用speak-tts插件的方式

1、安装speak-tts

npm install speak-tts

2.使用

import Speech from 'speak-tts'  //引入


初始化调用
this.speechInit();


speechInit(){
	this.speech = new Speech();
	this.speech.setLanguage('zh-CN');
	this.speech.init().then(()=>{
		console.log('语音播报初始化完成')
	})
},


this.speech.speak({text:item.newsContent}).then(()=>{
	this.speech.cancel(); //播放结束后调用
})

三、微信小程序可以采用微信提供的插件

1、添加插件 

2.由于我用的是uni-app,所以manifest.json添加配置

"mp-weixin" : {
        "appid" : "这个是小程序的appid",
        "setting" : {
            "urlCheck" : true,
            "es6" : true,
            "minified" : true,
            "postcss" : false
        },
        "optimization" : {
            "subPackages" : true
        },
        "usingComponents" : true,
        "plugins" : {
            "WechatSI" : {
                "version" : "0.3.5",
                "provider" : "填写刚才申请的appid"
            }
        }
    },

3.项目中使用

//条件编译  引入插件

// #ifdef MP-WEIXIN
var plugin = requirePlugin("WechatSI")
let manager = plugin.getRecordRecognitionManager()
// #endif
// #ifdef MP-WEIXIN
let _this=this 
plugin.textToSpeech({
	lang: "zh_CN",
	tts: true,
	content: playword,
	success: function(res) {
		_this.src = res.filename //这个就是生成成功的音频
		_this.smallRoutine(item,playword,index);
	},
	fail: function(res) {}
})
// #endif


//然后利用uni.createInnerAudioContext() 进行播放
//如果不会使用  请移步 https://uniapp.dcloud.net.cn/api/media/audio-context.html#createinneraudiocontext
this.innerAudioContext = uni.createInnerAudioContext()
this.innerAudioContext.pause();
this.innerAudioContext.volume = 1
this.innerAudioContext.src = this.src
this.innerAudioContext.play()
this.innerAudioContext.onEnded(()=>{
	//监听结束事件  做自己的处理逻辑
})

 4.如果合成音频不能播放可进行开发文档状态码查询 。(有时候可能文字过长无法合成报错,我这里可以提供一种思路,文字截取一段一段的) 

比如:(全部代码就不贴了)

let strlength =this.contentTxt.slice().length;
if(strlength>200){
	this.playAllNum=Math.ceil(strlength / 200); 
	playword = this.contentTxt.slice(0,200) 
}else{
	this.playAllNum=1; 
	playword =this.contentTxt
}

在开发uniapp时,如果需要实现文字语音的功能,可以有几种方案。其中一种方案是使用PDA自带的文字语音功能。这样可以避免使用网上收费的接口。具体的操作可以参考相关资料,根据PDA的具体型号系统版本来进行设置调用。 另外,还可以使用第三方库来实现文字语音的功能。比如可以使用"speak-tts"这个库来进行操作。首先需要导入Speech模块,然后进行初始化设置调用。可以设置需要的语言,然后进行初始化,初始化完成后就可以使用speak方法将文字换成语音并进行播放。播放结束后需要调用cancel方法停止播放。具体的代码示例可以参考上述的引用。 如果是在微信小程序中,可以使用微信提供的插件来实现文字语音的功能。可以根据微信提供的开发文档进行相应的设置调用。 另外,如果合成音频无法播放,可以根据开发文档中的状态码进行查询,根据错误信息进行相应的处理。有时候可能是由于文字过长导致无法合成,可以尝试将文字进行分段,一段一段地进行合成。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [uniapp文字语音最优方案](https://download.csdn.net/download/pxfpxf/88244365)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [uni-app/vue 文字语音朗读附小程序语音识别朗读)](https://blog.csdn.net/qq_42717015/article/details/131435881)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值