musicmouse/espmusicmouse/host_driver/player.py

83 lines
2.9 KiB
Python
Raw Permalink Normal View History

2021-11-27 22:05:48 +01:00
import vlc
class AudioPlayer:
def __init__(self, alsa_device=None):
params = ["-A", "alsa", "--alsa-audio-device", alsa_device] if alsa_device else []
self.instance = vlc.Instance(*params)
2021-12-15 22:47:28 +01:00
self.media_list_player = self.instance.media_list_player_new()
self.media_player = self.media_list_player.get_media_player()
evm = self.media_player.event_manager()
2021-12-16 23:39:13 +01:00
evm.event_attach(vlc.EventType.MediaPlayerStopped, self._callback)
evm2 = self.media_list_player.event_manager()
2021-12-28 11:42:39 +01:00
evm2.event_attach(vlc.EventType.MediaListPlayerPlayed, self._callback)
evm2.event_attach(vlc.EventType.MediaListPlayerStopped, self._callback)
2021-12-15 22:47:28 +01:00
self.on_playlist_end_callback = None
2021-12-26 11:30:56 +01:00
self.volume_min = None
self.volume_max = None
2021-12-15 22:47:28 +01:00
def create_playlist(self, files):
result = vlc.MediaList()
for e in files:
result.add_media(self.instance.media_new(e))
2021-12-28 11:42:39 +01:00
evm = result.event_manager()
evm.event_attach(vlc.EventType.MediaListEndReached,
2022-01-12 18:13:59 +01:00
lambda e: print("Ml CB", str(vlc.EventType(e.type))))
2021-12-28 11:42:39 +01:00
evm.event_attach(vlc.EventType.MediaListItemAdded,
2022-01-12 18:13:59 +01:00
lambda e: print("Ml ia CB", str(vlc.EventType(e.type))))
2021-12-28 11:42:39 +01:00
2021-12-15 22:47:28 +01:00
return result
def set_playlist(self, media_list):
self.media_list_player.set_media_list(media_list)
2021-12-28 11:42:39 +01:00
print("Setting media list of length ", media_list.count())
self.media_list_player.set_playback_mode(vlc.PlaybackMode.default)
2021-11-27 22:05:48 +01:00
2021-12-15 22:47:28 +01:00
def next(self):
2021-12-28 11:42:39 +01:00
return self.media_list_player.next()
2021-12-15 22:47:28 +01:00
def previous(self):
2021-12-28 11:42:39 +01:00
return self.media_list_player.previous()
2021-11-27 22:05:48 +01:00
def play(self):
2021-12-15 22:47:28 +01:00
self.media_list_player.play()
def play_from_start(self):
self.media_list_player.play_item_at_index(0)
def is_playing(self):
return self.media_list_player.is_playing()
2021-11-27 22:05:48 +01:00
def pause(self):
2021-12-15 22:47:28 +01:00
self.media_list_player.pause()
def _callback(self, event, *args, **kwargs):
2021-12-28 11:42:39 +01:00
eventStr = str(vlc.EventType(event.type))
print(f"Got vlc event type {event.type} {eventStr} , event {event}")
if event.type == vlc.EventType.MediaPlayerStopped:
2021-12-15 22:47:28 +01:00
if self.on_playlist_end_callback:
2021-12-28 11:42:39 +01:00
print("Calling playlist end cb")
2021-12-15 22:47:28 +01:00
self.on_playlist_end_callback()
2021-12-19 13:46:45 +01:00
#print("Callback from VLC", event, args, kwargs)
#print(event.meta_type, event.obj, event.type)
2021-12-15 23:14:59 +01:00
2021-12-26 11:30:56 +01:00
def set_volume(self, volume):
if self.volume_min and volume < self.volume_min:
volume = self.volume_min
if self.volume_max and volume > self.volume_max:
volume = self.volume_max
self.media_player.audio_set_volume(volume)
def set_volume_limits(self, vmin, vmax):
self.volume_min = vmin
self.volume_max = vmax
2021-12-15 23:14:59 +01:00
def change_volume(self, amount=1):
vol = self.media_player.audio_get_volume() + amount
2021-12-26 11:30:56 +01:00
self.set_volume(vol)