uniapp如何实现语音播报功能

在uniapp中如何实现语音播报功能?我尝试了一些方法但效果不理想,有没有成熟的解决方案或者插件推荐?需要兼容iOS和Android平台,最好能支持自定义语速和音量调节。另外,这个功能在微信小程序和H5端是否也能实现?求具体实现代码或示例。

2 回复

在uniapp中,可以使用uni.createInnerAudioContext()创建音频实例,加载语音文件后调用play()方法播放。也可以使用Web Speech API的speechSynthesis.speak(),但兼容性较差。建议将语音文件放在static目录,通过相对路径引用。


在uni-app中实现语音播报功能,可以通过以下两种方式实现:

方法一:使用Web Speech API(仅H5端)

// 适用于H5浏览器环境
speakText(text) {
  if ('speechSynthesis' in window) {
    const utterance = new SpeechSynthesisUtterance(text);
    // 设置语音参数
    utterance.rate = 1;    // 语速 0.1-10
    utterance.pitch = 1;   // 音调 0-2
    utterance.volume = 1;  // 音量 0-1
    utterance.lang = 'zh-CN'; // 中文
    
    // 开始播报
    speechSynthesis.speak(utterance);
    
    // 可选:监听播报结束
    utterance.onend = function() {
      console.log('语音播报结束');
    };
  } else {
    uni.showToast({
      title: '当前环境不支持语音播报',
      icon: 'none'
    });
  }
}

方法二:使用原生插件(全端支持)

1. 安装语音播报插件

# 安装uni-app官方语音插件
npm install @dcloudio/uni-plugin-speech

2. 在页面中使用

export default {
  methods: {
    // 开始语音播报
    startSpeech() {
      // #ifdef APP-PLUS
      plus.speech.startSpeaking('需要播报的文本内容', {
        volume: 1.0,
        rate: 1.0,
        pitch: 1.0
      }, function() {
        console.log('播报开始');
      }, function(e) {
        console.log('播报错误:', e);
      });
      // #endif
      
      // #ifdef H5
      this.speakText('需要播报的文本内容');
      // #endif
    },
    
    // 停止语音播报
    stopSpeech() {
      // #ifdef APP-PLUS
      plus.speech.stopSpeaking();
      // #endif
      
      // #ifdef H5
      if ('speechSynthesis' in window) {
        speechSynthesis.cancel();
      }
      // #endif
    }
  }
}

完整示例代码

<template>
  <view class="container">
    <input v-model="speechText" placeholder="输入要播报的文本" />
    <button @click="startSpeech">开始播报</button>
    <button @click="stopSpeech">停止播报</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      speechText: '欢迎使用语音播报功能'
    };
  },
  methods: {
    startSpeech() {
      if (!this.speechText.trim()) {
        uni.showToast({
          title: '请输入播报内容',
          icon: 'none'
        });
        return;
      }
      
      // #ifdef APP-PLUS
      plus.speech.startSpeaking(this.speechText);
      // #endif
      
      // #ifdef H5
      this.speakText(this.speechText);
      // #endif
    },
    
    stopSpeech() {
      // #ifdef APP-PLUS
      plus.speech.stopSpeaking();
      // #endif
      
      // #ifdef H5
      if ('speechSynthesis' in window) {
        speechSynthesis.cancel();
      }
      // #endif
    },
    
    // H5端语音播报
    speakText(text) {
      if ('speechSynthesis' in window) {
        const utterance = new SpeechSynthesisUtterance(text);
        utterance.lang = 'zh-CN';
        speechSynthesis.speak(utterance);
      } else {
        uni.showToast({
          title: '当前环境不支持语音播报',
          icon: 'none'
        });
      }
    }
  }
};
</script>

注意事项

  1. 平台差异

    • H5端依赖浏览器支持
    • App端需要原生模块支持
    • 小程序端通常不支持
  2. 权限配置(App端): 在manifest.json中配置语音权限:

    {
      "app-plus": {
        "permissions": {
          "Speech": {}
        }
      }
    }
    
  3. 兼容性处理

    • 使用条件编译处理多端差异
    • 添加错误处理和降级方案

这种方式可以在uni-app中实现跨平台的语音播报功能。

回到顶部