当前位置:   article > 正文

Python中语音播报的应用(简单版)_engine.runandwait

engine.runandwait

 用到的第三方库为pyttsx3,需要安装且导入使用。

  1. import pyttsx3
  2. engine = pyttsx3.init()
  3. engine.say("I will speak this text")
  4. engine.runAndWait()

 

 Changing Voice , Rate and Volume :

  1. import pyttsx3
  2. engine = pyttsx3.init() # object creation
  3. """ RATE"""
  4. rate = engine.getProperty('rate') # getting details of current speaking rate
  5. print (rate) #printing current voice rate
  6. engine.setProperty('rate', 125) # setting up new voice rate
  7. """VOLUME"""
  8. volume = engine.getProperty('volume') #getting to know current volume level (min=0 and max=1)
  9. print (volume) #printing current volume level
  10. engine.setProperty('volume',1.0) # setting up volume level between 0 and 1
  11. """VOICE"""
  12. voices = engine.getProperty('voices') #getting details of current voice
  13. #engine.setProperty('voice', voices[0].id) #changing index, changes voices. o for male
  14. engine.setProperty('voice', voices[1].id) #changing index, changes voices. 1 for female
  15. engine.say("Hello World!")
  16. engine.say('My current speaking rate is ' + str(rate))
  17. engine.runAndWait()
  18. engine.stop()
  19. """Saving Voice to a file"""
  20. # On linux make sure that 'espeak' and 'ffmpeg' are installed
  21. engine.save_to_file('Hello World', 'test.mp3')
  22. engine.runAndWait()

我自己的demo演示:

  1. # 工具模块
  2. import pyttsx3 #导入
  3. #创建 初始化
  4. engine = pyttsx3.init()
  5. #说话
  6. engine.say('晋睿敏,你是我遥遥万里牵挂的猪,哈哈')
  7. #运行
  8. engine.runAndWait()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/778796
推荐阅读
相关标签
  

闽ICP备14008679号