-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdel.py
More file actions
32 lines (25 loc) · 951 Bytes
/
del.py
File metadata and controls
32 lines (25 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from gtts import gTTS
import os
from playsound import playsound
def text_to_speech(text, lang='en'):
try:
# Create a gTTS object
tts = gTTS(text=text, lang=lang)
# Save the audio file
audio_file = "speech.mp3"
tts.save(audio_file)
print(f"Audio file saved as {audio_file}")
# Check if the file exists
if os.path.exists(audio_file):
# Play the audio file
playsound(audio_file)
print("Audio played successfully")
# Optionally, remove the audio file after playing
print("Audio file removed after playing")
else:
print(f"Error: The file {audio_file} was not found.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
text = "Hello, this is a test of the Google Text-to-Speech library."
text_to_speech(text)