> For the complete documentation index, see [llms.txt](https://shenjunhong.gitbook.io/blog/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://shenjunhong.gitbook.io/blog/za-wen/qian-duan-wen-zi-zhuan-yu-yin-bo-fang.md).

# 前端文字转语音播放

### SpeechSynthesisUtterance

纯 web

```js
  this.speech = new SpeechSynthesisUtterance();
  let speech = this.speech;
  speech.text = textMsg.value // 获取并设置说话时的文本
  speechSynthesis.speak(speech);
```

### 第三方文字转语音播放

1. 百度文字转语音开放 api

```js
const text = '测试文字'
this.testAudio = new Audio()
this.testAudio.src = 'https://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=5&&text=' + text
this.testAudio.muted = false
this.testAudio.play()

```
