Skip to content

Instantly share code, notes, and snippets.

@Xennis
Created August 23, 2017 18:45
Show Gist options
  • Save Xennis/4829500b9fe843f247685ae704d7d6a5 to your computer and use it in GitHub Desktop.
Save Xennis/4829500b9fe843f247685ae704d7d6a5 to your computer and use it in GitHub Desktop.
xennis-radio
/media/
############
## Python
############
*.pyc
############
## IDEA
############
.idea/
*.iml
import os
from MediaApi import MediaApi
MEDIA_DIR = 'media'
MUSIC_DIR = os.path.join(MEDIA_DIR, 'music')
MUSIC_CATEGORIES = {
'asmr': os.path.join(MUSIC_DIR, 'asmr')
}
api = MediaApi()
api.play_random_music(MUSIC_CATEGORIES['asmr'])
'''
from evdev import list_devices, InputDevice, categorize, ecodes
devices = [InputDevice(fn) for fn in list_devices()]
for device in devices:
print(device.fn, device.name, device.phys)
device = InputDevice('/dev/input/event12')
for event in device.read_loop():
if event.type == ecodes.EV_KEY:
if event.code == ecodes.KEY_VOLUMEDOWN:
print('vol up')
elif event.code == ecodes.KEY_VOLUMEDOWN:
print('vol down')
'''
import os
import random
from MediaPlayer import MediaPlayer
class MediaApi():
MUSIC_FILES_EXTENSIONS = ['mp3']
def __init__(self):
self.player = MediaPlayer()
def play_random_music(self, path):
files = [fn for fn in os.listdir(path) if any(fn.endswith(ext) for ext in self.MUSIC_FILES_EXTENSIONS)]
randomIndex = random.randint(0, len(files) - 1);
file = os.path.join(path, files[randomIndex]);
self.player.play(file)
import vlc
import time
class MediaPlayer():
def __init__(self):
self.player = vlc.MediaPlayer()
def play(self, file):
media = vlc.Media(file)
self.player.set_media(media)
self.player.play()
time.sleep(1 * 1000)
while self.player.is_playing():
time.sleep(1 * 1000)
def set_volume(self, value):
result = self.player.audio_set_volume(value)
return result == 0
def get_volume(self):
return self.player.audio_get_volume()
evdev==0.6.4
python-vlc==1.1.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment