SunFounder PiCar-X Kit
(continued from previous page)
tts = TTS()
manual = '''
Input key to call the function!
space: Play sound effect (Car horn)
c: Play sound effect with threads
t: Text to speak
q: Play/Stop Music
'''
def main():
print(manual)
flag_bgm = False
music.music_set_volume(20)
tts.lang("en-US")
while True:
key = readchar.readkey()
key = key.lower()
if key == "q":
flag_bgm = not flag_bgm
if flag_bgm is True:
music.music_play('../musics/slow-trail-Ahjay_Stelino.mp3')
else:
music.music_stop()
elif key == readchar.key.SPACE:
music.sound_play('../sounds/car-double-horn.wav')
sleep(0.05)
elif key == "c":
music.sound_play_threading('../sounds/car-double-horn.wav')
sleep(0.05)
elif key == "t":
words = "Hello"
tts.say(words)
if __name__ == "__main__":
main()
How it works?
Functions related to background music include these:
• music = Music() : Declare the object.
• music.music_set_volume(20) : Set the volume, the range is 0~100.
• music.music_play('../musics/slow-trail-Ahjay_Stelino.mp3') : Play music files, here is the slow-
trail-Ahjay_Stelino.mp3 file under the ../musics path.
• music.music_stop() : Stop playing background music.
56 Chapter 4. Play with Python