83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
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)
|
|
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()
|
|
evm.event_attach(vlc.EventType.MediaPlayerStopped, self._callback)
|
|
|
|
evm2 = self.media_list_player.event_manager()
|
|
evm2.event_attach(vlc.EventType.MediaListPlayerPlayed, self._callback)
|
|
evm2.event_attach(vlc.EventType.MediaListPlayerStopped, self._callback)
|
|
|
|
self.on_playlist_end_callback = None
|
|
|
|
self.volume_min = None
|
|
self.volume_max = None
|
|
|
|
def create_playlist(self, files):
|
|
result = vlc.MediaList()
|
|
for e in files:
|
|
result.add_media(self.instance.media_new(e))
|
|
|
|
evm = result.event_manager()
|
|
evm.event_attach(vlc.EventType.MediaListEndReached,
|
|
lambda e: print("Ml CB", str(vlc.EventType(e.type))))
|
|
evm.event_attach(vlc.EventType.MediaListItemAdded,
|
|
lambda e: print("Ml ia CB", str(vlc.EventType(e.type))))
|
|
|
|
return result
|
|
|
|
def set_playlist(self, media_list):
|
|
self.media_list_player.set_media_list(media_list)
|
|
print("Setting media list of length ", media_list.count())
|
|
self.media_list_player.set_playback_mode(vlc.PlaybackMode.default)
|
|
|
|
def next(self):
|
|
return self.media_list_player.next()
|
|
|
|
def previous(self):
|
|
return self.media_list_player.previous()
|
|
|
|
def play(self):
|
|
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()
|
|
|
|
def pause(self):
|
|
self.media_list_player.pause()
|
|
|
|
def _callback(self, event, *args, **kwargs):
|
|
eventStr = str(vlc.EventType(event.type))
|
|
print(f"Got vlc event type {event.type} {eventStr} , event {event}")
|
|
if event.type == vlc.EventType.MediaPlayerStopped:
|
|
if self.on_playlist_end_callback:
|
|
print("Calling playlist end cb")
|
|
self.on_playlist_end_callback()
|
|
#print("Callback from VLC", event, args, kwargs)
|
|
#print(event.meta_type, event.obj, event.type)
|
|
|
|
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
|
|
|
|
def change_volume(self, amount=1):
|
|
vol = self.media_player.audio_get_volume() + amount
|
|
self.set_volume(vol)
|