-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.py
73 lines (61 loc) · 2.17 KB
/
music.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
import pafy
import vlc
import time
import json
import random
def playMusic(Instance, player):
#Read the playlist from json
with open('playlist.json') as f:
data = json.load(f)
musics = data["music"] #Get the playlist as JSON array
random.shuffle(musics) #Shuffle the music playlist
length = len(musics)
#Load and play each music from the playlist
for m in musics:
#Extract music data
name = m["name"]
url = m["url"]
#Load audio from youtube url
try:
video = pafy.new(url)
except: #Error
print("Unable to play \""+ name + "\": Incorrect URL or no internet access")
media = vlc.MediaPlayer("sounds/nomusic.mp3")
media.play()
time.sleep(12)
continue
#Get audio source from YouTube
source = video.getbestaudio()
playurl = source.url
#Load music into the player
Media = Instance.media_new(playurl)
Media.get_mrl()
player.set_media(Media)
player.play()
#Show play data
print("Playing:",name)
time.sleep(2)
duration = player.get_length() / 1000
print("Duration:", duration)
#Wait until music finished or playlist updated
playlist_updated = False
while(player.get_state()!=vlc.State.Stopped and player.get_state()!=vlc.State.Ended):
#Read playlist.json again
try:
with open('playlist.json') as f:
data_new = json.load(f)
except:
None #Ignore error due to playlist.json being updated
#Check if playlist being updated
if(len(json.dumps(data)) != len(json.dumps(data_new))):
print("Playlist updated!")
playlist_updated = True
data = data_new
length = len(musics)
player.stop()
time.sleep(1) #Reduce strain on CPU
#Refresh music with the new playlist
if playlist_updated:
break
print("Ended:", name)
time.sleep(1)