VoiceTest.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <style lang="scss" scoped>
  2. .home-page{
  3. }
  4. </style>
  5. <template>
  6. <div class="home-page">
  7. <el-input type="textarea" autosize placeholder="请输入内容" v-model="voiceSet.text"></el-input>
  8. <label>语速:</label><el-input-number v-model="voiceSet.rate" :precision="1" :step="0.1" :max="10"></el-input-number>
  9. &nbsp;&nbsp;&nbsp;
  10. <label>音高:</label><el-input-number v-model="voiceSet.pitch" :step="0.1" :min="0" :max="2"></el-input-number>
  11. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  12. <el-button type="primary" @click="voicePlay">开 始</el-button>
  13. &nbsp;&nbsp;&nbsp;
  14. <el-button type="primary" @click="voicePause">暂 停</el-button>
  15. &nbsp;&nbsp;&nbsp;
  16. <el-button type="primary" @click="voiceResume">继 续</el-button>
  17. &nbsp;&nbsp;&nbsp;
  18. <el-button type="primary" @click="voiceCancel">取 消</el-button>
  19. </div>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'VoiceTest',
  24. components: {
  25. },
  26. data () {
  27. return {
  28. voiceSet: {
  29. text: 'The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with.',
  30. rate: 0.5,
  31. pitch: 1
  32. },
  33. msg: new SpeechSynthesisUtterance(), //构建语音合成实例
  34. syn: window.speechSynthesis
  35. }
  36. },
  37. created() {
  38. },
  39. methods: {
  40. voicePlay() {
  41. this.msg.text = this.voiceSet.text;
  42. this.msg.lang = 'en';
  43. this.msg.volume = '1';
  44. this.msg.rate = this.voiceSet.rate;
  45. this.msg.pitch = this.voiceSet.pitch;
  46. this.syn.speak(this.msg);
  47. // console.log(speechSynthesis.getVoices()); //返回浏览器支持的语音包列表数组
  48. },
  49. voicePause(){
  50. this.syn.pause()
  51. },
  52. voiceResume(){
  53. this.syn.resume()
  54. },
  55. voiceCancel(){
  56. this.syn.cancel()
  57. },
  58. }
  59. }
  60. </script>