-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_audio.py
48 lines (37 loc) · 1.21 KB
/
random_audio.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
#! /usr/bin/python3
from random import choice
from omxplayer.player import OMXPlayer
from glob import glob
from os import listdir
from os.path import isfile, join
from config import PLAYING
from config import FILETYPES
class RandomAudio(object):
"""
Random Audio class
"""
def __init__(self, audio_directory):
self.audio_directory = audio_directory
def get_audio_list(self):
audio_directory = self.audio_directory
files_list = []
for filetype in FILETYPES:
files_list.extend(glob(join(audio_directory, '*.' + filetype)))
self.files_list = files_list
return files_list
def select_audio(self):
try:
self.files_list
return choice(self.files_list)
except AttributeError:
return choice(self.get_audio_list())
def play_random_audio(self):
audio_file = join(self.audio_directory, self.select_audio())
assert(audio_file != None)
try:
player = OMXPlayer(audio_file)
print("Playing `" + audio_file + "`")
self.player = player
self.state = PLAYING
except:
print("Error playing file " + audio_file)