audio.py 438 B

1234567891011121314151617
  1. from gtts import gTTS
  2. import os
  3. def text_to_speech(text, language='zh-cn'):
  4. # Create a gTTS object
  5. tts = gTTS(text=text, lang=language, slow=False)
  6. # Save the speech as an audio file
  7. tts.save("output.mp3")
  8. # Play the audio file
  9. os.system("mpg321 output.mp3") # Make sure you have mpg321 installed
  10. if __name__ == "__main__":
  11. text = "你好,这是在进行自动驾驶测试。"
  12. text_to_speech(text)