60 likes | 74 Views
Learn how to make your computer speak in different languages, create temporary audio files, and automate text-to-speech using Python. Install gTTS and Pygame libraries, generate speech, and play audio files. Enhance your coding skills with this helpful guide.
E N D
如何讓電腦說話 ? • 安裝 gTTS功能套件 pip install gTTS from gtts import gTTS tts=gTTS(text=“Hello?”,lang=“en”) tts.save(“hello.mp3”)
如何讓電腦說中文話 ? from gtts import gTTS tts=gTTS(text=“你好嗎?”,lang=“zh-tw”) tts.save(“hello.mp3”)
如何讓電腦主動說話 ? • 安裝 pygame功能套件 pip install pygame from pygame import mixer mixer.init() mixer.music.load(“hello.mp3”) mixer.music.play()
如何讓電腦隨機產生暫存檔? from gtts import gTTS from pygame import mixer import tempfile with tempfile.NamedTemporaryFile(delete=True) as fp: tts=gTTS(text="今天,你好嗎?",lang="zh-tw") tts.save("{}.mp3".format(fp.name)) mixer.init() mixer.music.load("{}.mp3".format(fp.name)) mixer.music.play()
如何可以重複使用電腦語音播放 ? from gtts import gTTS from pygame import mixer import tempfile defspeak(sentence): with tempfile.NamedTemporaryFile(delete=True) as fp: tts=gTTS(text=sentence,lang="zh-tw") tts.save("{}.mp3".format(fp.name)) mixer.init() mixer.music.load("{}.mp3".format(fp.name)) mixer.music.play() speak("學習過程是痛苦的,但成果是甜美的")