-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
executable file
·134 lines (112 loc) · 4.44 KB
/
player.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
from typing import Callable
from vlc import Instance as vlcInstance, MediaList, MediaListPlayer, Event, EventType, State
from datetime import timedelta
from time import sleep
class MusicPlayer:
"""Simple LibVLC based music player
:Author: Vivien Richter <vivien-richter@outlook.de>
:Version: 2.0.0
:License: `Creative Commons Attribution 4.0 International <https://creativecommons.org/licenses/by/4.0>`_
:Repository: `GitHub <https://github.com/vivi90/py-libvlc-player.git>`_
"""
def __init__(self, mrls: tuple[str, ...] = None, options: str = "", trackChangedEventCallback: Callable[[str, str, str, str, str], None] = None, positionChangedEventCallback: Callable[[float], None] = None) -> None:
"""Constructor
:param mrls: Media ressource locators
:param trackChangedEventCallback: User defined callback, that's called at an track changed event and expects five parameters
:param positionChangedEventCallback: User defined callback, that's called at an position changed event and expects one parameter
"""
self.player = MediaListPlayer(vlcInstance(options))
self.player.event_manager().event_attach(
EventType.MediaListPlayerNextItemSet,
self.trackChanged
)
self.player.get_media_player().event_manager().event_attach(
EventType.MediaPlayerTimeChanged,
self.positionChanged
)
self.trackChangedEventCallback = trackChangedEventCallback
self.positionChangedEventCallback = positionChangedEventCallback
self.mrls = mrls
def trackChanged(self, event):
'''Track changed event'''
media = self.player.get_media_player().get_media()
if media.get_meta(1) is not None and self.trackChangedEventCallback is not None:
self.trackChangedEventCallback(
media.get_meta(0), # Title
media.get_meta(1), # Artist
media.get_meta(6), # Description
media.get_meta(10), # URL
media.get_meta(15) # Artlink
)
def positionChanged(self, event):
'''Position changed event'''
if self.positionChangedEventCallback is not None:
self.positionChangedEventCallback(
self.player.get_media_player().get_time() # Current position in ms
)
@property
def mrls(self) -> tuple[str, ...]:
'''Media ressource locators of the current playlist'''
return self._mrls
@mrls.setter
def mrls(self, mrls: tuple[str, ...] = None) -> None:
'''Loads a new playlist'''
self._mrls = mrls
self.player.set_media_list(MediaList(self._mrls))
@property
def isPlaying(self) -> bool:
'''Playing state'''
return True if self.player.get_state() == State.Playing else False
@property
def isBusy(self) -> bool:
'''Busy state'''
currentState = self.player.get_state()
return True if currentState == State.Opening or currentState == State.Buffering else False
@property
def isFailed(self) -> bool:
'''Failing state'''
return True if self.player.get_state() == State.Error else False
def play(self) -> None:
'''Play action'''
self.player.play()
def pause(self) -> None:
'''Pause action'''
self.player.pause()
def stop(self) -> None:
'''Stop action'''
self.player.stop()
def nextTrack(self) -> None:
'''Next track action'''
self.player.next()
def previousTrack(self) -> None:
'''Previous track action'''
self.player.previous()
# Usage example
playlist = [
"https://www.youtube.com/watch?v=6qEzh3wKVJc",
"https://www.youtube.com/watch?v=UfVF3NWzeAY"
]
def onTrackChanged(title: str, artist: str, description: str, URL: str, artURL: str) -> None:
'''Print track infos'''
print(artist, " - ", title)
print(description)
print(artURL)
print(URL)
def onPositionChanged(currentPosition: float) -> None:
'''Print current position'''
print(
str(timedelta(
seconds=currentPosition / 1000
)).split(".")[0]
)
musicPlayer = MusicPlayer(
None,
"--no-ts-trust-pcr --ts-seek-percent --no-video -q",
onTrackChanged,
onPositionChanged
)
musicPlayer.mrls = playlist
musicPlayer.play()
while musicPlayer.isPlaying or musicPlayer.isBusy:
sleep(1) # Keeps this thread waiting until the music ends