uniapp开发微信小程序,使用微信同声传译插件,实现语音识别输入文本

第一步:现在微信小程序管理后台:“设置”-》“第三方设置”-》“插件管理”中添加插件。

但是这个地方,没有搜索到插件,就到微信服务市场去搜微信服务市场

 搜索到以后添加到需要的小程序里面,然后返回管理中心查看,就可以看到了

第二步:在配置文件中引入插件

第三步:在需要使用的页面引入:

var plugin = requirePlugin("WechatSI")
let manager = plugin.getRecordRecognitionManager() 

页面代码:

<button @touchstart="streamRecord" @touchend="endStreamRecord" form-type="submit" type="primary" class="fc-white">语音识别按钮</button>
            <view> 语音识别内容:{{currentText}} </view>

js代码:

data中定义:data() {
            return {
                currentText:""
            }
        },

在methods中定义方法:

streamRecord: function(){
                console.log('=======开始====')
               manager.start({
                 lang: 'zh_CN',
               })
            },
            endStreamRecord: function(){
                console.log('=======结束====')
                manager.stop()
            },
            initRecord: function(){
               //有新的识别内容返回,则会调用此事件
               manager.onRecognize = (res) => {
                 let text = res.result
                 this.currentText = text
               }
               // 识别结束事件
               manager.onStop = (res) => {
                 let text = res.result
                 if(text == '') {
                    console.log('没有说话')
                  return
                 }
                 this.currentText = text
               }
            },

在onLoad中调用初始化:

onLoad() {
            this.initRecord()
        },

然后就可以正常使用了。

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的Python程序,可以将音频文件中的语音换为文本: 1. 首先,需要安装SpeechRecognition模块。可以使用pip命令来安装。在命令行中输入以下命令: `pip install SpeechRecognition` 2. 导入SpeechRecognition模块和AudioFile模块 ```python import speech_recognition as sr from pydub import AudioSegment from pydub.silence import split_on_silence ``` 3. 定义一个函数,将音频文件分割为小段,以便于识别。这里使用了pydub库中的split_on_silence函数,将音频文件根据静音部分分割成多个小段。 ```python def get_large_audio_transcription(path): r = sr.Recognizer() sound = AudioSegment.from_wav(path) chunks = split_on_silence(sound, min_silence_len=500, silence_thresh=sound.dBFS-14, keep_silence=500, ) folder_name = "audio-chunks" if not os.path.isdir(folder_name): os.mkdir(folder_name) whole_text = "" for i, audio_chunk in enumerate(chunks, start=1): chunk_filename = os.path.join(folder_name, f"chunk{i}.wav") audio_chunk.export(chunk_filename, format="wav") with sr.AudioFile(chunk_filename) as source: audio_listened = r.record(source) try: text = r.recognize_google(audio_listened) except sr.UnknownValueError as e: print("Error:", str(e)) else: text = f"{text.capitalize()}. " print(chunk_filename, ":", text) whole_text += text return whole_text ``` 4. 调用该函数并传入音频文件的路径,即可将音频换为文本。 ```python path = "path/to/audio_file.wav" print("\nFull text:", get_large_audio_transcription(path)) ``` 完整代码示例: ```python import os import speech_recognition as sr from pydub import AudioSegment from pydub.silence import split_on_silence def get_large_audio_transcription(path): r = sr.Recognizer() sound = AudioSegment.from_wav(path) chunks = split_on_silence(sound, min_silence_len=500, silence_thresh=sound.dBFS-14, keep_silence=500, ) folder_name = "audio-chunks" if not os.path.isdir(folder_name): os.mkdir(folder_name) whole_text = "" for i, audio_chunk in enumerate(chunks, start=1): chunk_filename = os.path.join(folder_name, f"chunk{i}.wav") audio_chunk.export(chunk_filename, format="wav") with sr.AudioFile(chunk_filename) as source: audio_listened = r.record(source) try: text = r.recognize_google(audio_listened) except sr.UnknownValueError as e: print("Error:", str(e)) else: text = f"{text.capitalize()}. " print(chunk_filename, ":", text) whole_text += text return whole_text path = "path/to/audio_file.wav" print("\nFull text:", get_large_audio_transcription(path)) ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值