-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay_music.py
55 lines (44 loc) · 1.54 KB
/
play_music.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
import os
import sys
import random
from tts import tts
def mp3gen(music_path):
"""
This function finds all the MP3 files in a folder and its subfolders and
returns a list:
"""
music_list = []
for root, dirs, files in os.walk(music_path):
for filename in files:
if os.path.splitext(filename)[1] == ".mp3":
music_list.append(os.path.join(root, filename.lower()))
return music_list
def music_player(file_name):
"""
This function takes the name of a music file as an argument and plays it
depending on the OS.
"""
if sys.platform == 'darwin':
player = "afplay '" + file_name + "'"
return os.system(player)
elif sys.platform == 'linux2' or sys.platform == 'linux':
player = "mpg123 '" + file_name + "'"
return os.system(player)
def play_random(music_path):
try:
music_listing = mp3gen(music_path)
music_playing = random.choice(music_listing)
tts("Now playing: " + music_playing)
music_player(music_playing)
except IndexError as e:
tts('No music files found.')
print("No music files found: {0}".format(e))
def play_specific_music(speech_text, music_path):
words_of_message = speech_text.split()
words_of_message.remove('play')
cleaned_message = ' '.join(words_of_message)
music_listing = mp3gen(music_path)
for i in range(0, len(music_listing)):
if cleaned_message in music_listing[i]:
music_player(music_listing[i])