Browse Source

Merge branch 'Music-Player_fix' of shuvit/shuvit into dev

shuvit 5 years ago
parent
commit
c7c3f1e0c1

BIN
Test/Music/Divineinfinite - Bang This.mp3 View File


BIN
Test/Music/Just 1 Soul - Growing.mp3 View File


+ 1
- 0
Test/Music/Test.txt View File

@@ -0,0 +1 @@
1
+Hello, friend

BIN
Test/Music/alien-spaceship_daniel_simion.wav View File


+ 166
- 173
music_player.py View File

@@ -1,173 +1,166 @@
1
-#shuvit.org
2
-#mp3 player
3
-
4
-import bge
5
-import os
6
-import aud
7
-import random
8
-from tinytag import TinyTag
9
-
10
-directory = bge.logic.expandPath('//Music')
11
-file_list = os.listdir(directory) 
12
-selected = random.choice(file_list)
13
-full_path = os.path.join(directory, selected)
14
-
15
-dict = bge.logic.globalDict
16
-dict['mu_current_song'] = ''
17
-dict['mu_stopped'] = 0
18
-dict['mu_playlist'] = ''
19
-dict['mu_lib'] = ''
20
-dict['change_track'] = 0
21
-dict['mu_track_time'] = 0
22
-#dict['mu_last_track'] = ''
23
-#dict['mu_artist'] = ''
24
-#dict['mu_title'] = ''
25
-
26
-keyboard = bge.logic.keyboard
27
-JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
28
-
29
-def stop_music(sound):
30
-
31
-    print("Stop music")    
32
-    sound.pauseSound()
33
-    dict['mu_stopped'] = 1
34
-    dict['music_player'] = 0
35
-    
36
-def play_music():
37
-
38
-    print("Play music") 
39
-    full_path = os.path.join(directory, dict['mu_current_song'])
40
-    sound = bge.logic.getCurrentController().actuators['music_player']
41
-    sound.sound = aud.Factory(full_path)
42
-    sound.stopSound()
43
-    sound.startSound()
44
-    dict['mu_stopped'] = 0
45
-    dict['change_track'] = 0
46
-    dict['mu_track_time'] = 0 
47
-    dict['music_player'] = 1  
48
-
49
-    try:
50
-        tag = TinyTag.get(full_path)
51
-        print('Playing: ', 'Artist: %s' % tag.artist, 'Track: %s' % tag.title)
52
-        dict['mu_artist'] = tag.artist
53
-        dict['mu_title'] = tag.title
54
-        info = ('Music > ', 'Artist: %s' % tag.artist, 'Track: %s' % tag.title)
55
-        print(info)
56
-        dict['music_info'] = info
57
-    except:
58
-        print("Track has no tag.") 
59
-    print(dict['mu_title'], 'mu_title')
60
-def next_track():
61
-
62
-    print("next track")
63
-    dict = bge.logic.globalDict
64
-    plist = dict['mu_playlist']
65
-    cur_song = dict['mu_current_song']
66
-    pl_length = len(dict['mu_playlist'])    
67
-
68
-    if pl_length > 1:
69
-        try:
70
-            plist.remove(cur_song)
71
-        except:
72
-            pass    
73
-
74
-    if pl_length == 1:    
75
-        print('rebuilding playlist')
76
-        file_list = os.listdir(directory) 
77
-        plist = file_list
78
-
79
-    selected = random.choice(plist)
80
-    dict['mu_last_track'] = dict['mu_current_song']
81
-    dict['mu_current_song'] = selected
82
-    dict['mu_playlist'] = plist
83
-    dict['change_track'] = 0
84
-    dict['mu_track_time'] = 0
85
-    play_music()
86
-    
87
-    
88
-
89
-    #dict['music_info'] = info    
90
-
91
-def previous_track():
92
-
93
-    print("previous track") 
94
-    dict['mu_current_song'] = dict['mu_last_track']
95
-    play_music()
96
-
97
-def initer(cont):
98
-    #cont = bge.logic.getCurrentController()
99
-    own = cont.owner
100
-    dict = bge.logic.globalDict
101
-    if 'playback' not in dict:
102
-        dict['playback'] = True
103
-    if 'mp_inited' not in own:
104
-        own['mp_inited'] = True
105
-        print('Initializing music player')
106
-        
107
-        file_list = os.listdir(directory) 
108
-        dict['mu_lib'] = file_list
109
-        dict['mu_playlist'] = file_list
110
-        dict['change_track'] = 0
111
-        selected = random.choice(file_list)
112
-        dict['mu_current_song'] = selected
113
-        print('Current Track: ', dict['mu_current_song'])
114
-        full_path = os.path.join(directory, selected)
115
-
116
-        try:
117
-            tag = TinyTag.get(full_path)
118
-            print('Artist: %s' % tag.artist, 'Track: %s' % tag.title)
119
-            dict['mu_artist'] = tag.artist
120
-            dict['mu_title'] = tag.title            
121
-        except:
122
-            print("Track has no tag.")     
123
-
124
-def check_status():
125
-
126
-    sound = bge.logic.getCurrentController().actuators['music_player']
127
-    cur_song = dict['mu_current_song']
128
-    full_path = os.path.join(directory, dict['mu_current_song'])
129
-    sound = bge.logic.getCurrentController().actuators['music_player']
130
-    sound.sound = aud.Factory(full_path)  
131
-
132
-    if sound.time < dict['mu_track_time'] and dict['mu_stopped'] == 0 and dict['change_track'] == 0:
133
-        print('song over, getting new one')
134
-        dict['change_track'] = 1
135
-        dict['mu_track_time'] = 0
136
-    else:
137
-        dict['change_track'] = 0
138
-    dict['mu_track_time'] = sound.time    
139
-
140
-def main(cont):
141
-
142
-    dict = bge.logic.globalDict        
143
-    cur_song = dict['mu_current_song']    
144
-    full_path = os.path.join(directory, dict['mu_current_song'])
145
-    sound = bge.logic.getCurrentController().actuators['music_player']
146
-    sound.sound = aud.Factory(full_path)
147
-    sound.mode = 1
148
-    initer(cont)
149
-    if dict['music_player'] != 0:
150
-        if dict['mu_stopped'] == 0:        
151
-            sound.startSound()
152
-        if dict['change_track'] == 1:
153
-            dict['change_track'] = 0
154
-            next_track() 
155
-
156
-    plist = dict['mu_playlist']  
157
-    pl_length = len(dict['mu_playlist'])    
158
-    if pl_length == 0:
159
-        dict['mu_playlist'] = dict['mu_lib']        
160
-    #allow controlls while paused or in replay    
161
-    if dict['npause']  == 1 or dict['playback'] == True:
162
-        if dict['yBut'] == False and dict['last_yBut'] == True:
163
-            if dict['mu_stopped'] == 1:
164
-                play_music()    
165
-            else:
166
-                stop_music(sound)  
167
-        if dict['lBump'] == False and dict['last_lBump'] == True:
168
-            previous_track()
169
-        if dict['rBump'] == False and dict['last_rBump'] == True:
170
-            next_track()                
171
-            
172
-    
173
-#main()        
1
+#shuvit.org
2
+#mp3 player
3
+
4
+import bge
5
+import os
6
+import aud
7
+import random
8
+import glob
9
+from tinytag import TinyTag
10
+
11
+# dict
12
+dict = bge.logic.globalDict
13
+dict['mu_current_song'] = ''
14
+dict['mu_stopped'] = 1
15
+dict['change_track'] = 0
16
+dict['mu_track_time'] = 0
17
+
18
+# create file path
19
+directory = bge.logic.expandPath("//")
20
+file_name = directory + "Music\\*.mp3" # Test directory "Test\\Music\\*.mp3"
21
+file_list = glob.glob(file_name)
22
+selected = random.choice(file_list)
23
+full_path = os.path.join(directory, selected)
24
+dict['mu_playlist'] = file_list
25
+dict['mu_lib'] = file_list
26
+
27
+# inputs
28
+keyboard = bge.logic.keyboard
29
+JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
30
+
31
+def initer(cont):
32
+    own = cont.owner
33
+    dict = bge.logic.globalDict
34
+    if 'mplayback' not in dict:
35
+        dict['mplayback'] = True
36
+    if 'mp_inited' not in own:
37
+        own['mp_inited'] = True
38
+        print('Initializing music player')
39
+        
40
+        dict['mu_lib'] = file_list
41
+        dict['mu_playlist'] = file_list
42
+        dict['change_track'] = 0
43
+        selected = random.choice(file_list)
44
+        dict['mu_current_song'] = selected
45
+        print('Current Track: ', dict['mu_current_song'])
46
+        full_path = os.path.join(directory, selected)
47
+
48
+        try:
49
+            tag = TinyTag.get(full_path)
50
+            print('Artist: %s' % tag.artist, 'Track: %s' % tag.title)
51
+            dict['mu_artist'] = tag.artist
52
+            dict['mu_title'] = tag.title            
53
+        except:
54
+            print("Track has no tag.")  
55
+            
56
+def stop_music(sound):
57
+
58
+    print("Stop music")    
59
+    sound.pauseSound()
60
+    dict['mu_stopped'] = 1
61
+    dict['music_player'] = 0
62
+    
63
+def play_music():
64
+
65
+    print("Play music") 
66
+    full_path = os.path.join(directory, dict['mu_current_song'])
67
+    sound = bge.logic.getCurrentController().actuators['music_player']
68
+    sound.sound = aud.Factory(full_path)
69
+    sound.stopSound()
70
+    sound.startSound()
71
+    dict['mu_stopped'] = 0
72
+    dict['change_track'] = 0
73
+    dict['mu_track_time'] = 0 
74
+    dict['music_player'] = 1  
75
+    try:
76
+        tag = TinyTag.get(full_path)
77
+        print('Playing: ', 'Artist: %s' % tag.artist, 'Track: %s' % tag.title)
78
+        dict['mu_artist'] = tag.artist
79
+        dict['mu_title'] = tag.title
80
+        info = ('Music > ', 'Artist: %s' % tag.artist, 'Track: %s' % tag.title)
81
+        dict['music_info'] = info
82
+    except:
83
+        print("Track has no tag.") 
84
+    print(dict['mu_title'], 'mu_title')
85
+    
86
+def next_track():
87
+
88
+    print("next track")
89
+    dict = bge.logic.globalDict
90
+    plist = dict['mu_playlist']
91
+    cur_song = dict['mu_current_song']
92
+    pl_length = len(dict['mu_playlist'])    
93
+
94
+    if pl_length > 1:
95
+        try:
96
+            plist.remove(cur_song)
97
+        except:
98
+            pass    
99
+
100
+    if pl_length == 1:    
101
+        print('rebuilding playlist')
102
+        file_list = glob.glob(file_name)
103
+        plist = file_list
104
+
105
+    selected = random.choice(plist)
106
+    dict['mu_last_track'] = dict['mu_current_song']
107
+    dict['mu_current_song'] = selected
108
+    dict['mu_playlist'] = plist
109
+    dict['change_track'] = 0
110
+    dict['mu_track_time'] = 0
111
+    play_music()
112
+
113
+def previous_track():
114
+
115
+    print("previous track") 
116
+    dict['mu_current_song'] = dict['mu_last_track']
117
+    play_music()   
118
+
119
+def check_status():
120
+
121
+    sound = bge.logic.getCurrentController().actuators['music_player']
122
+    cur_song = dict['mu_current_song']
123
+    full_path = os.path.join(directory, dict['mu_current_song'])
124
+    sound = bge.logic.getCurrentController().actuators['music_player']
125
+    sound.sound = aud.Factory(full_path)  
126
+
127
+    if sound.time < dict['mu_track_time'] and dict['mu_stopped'] == 0 and dict['change_track'] == 0:
128
+        print('song over, getting new one')
129
+        dict['change_track'] = 1
130
+        dict['mu_track_time'] = 0
131
+    else:
132
+        dict['change_track'] = 0
133
+    dict['mu_track_time'] = sound.time    
134
+
135
+def main(cont):
136
+
137
+    dict = bge.logic.globalDict        
138
+    cur_song = dict['mu_current_song']    
139
+    full_path = os.path.join(directory, dict['mu_current_song'])
140
+    sound = bge.logic.getCurrentController().actuators['music_player']
141
+    sound.sound = aud.Factory(full_path)
142
+    sound.mode = 1
143
+    initer(cont)
144
+    if dict['music_player'] != 0:
145
+        if dict['mu_stopped'] == 1:        
146
+            play_music()
147
+        if dict['change_track'] == 1:
148
+            dict['change_track'] = 0
149
+            next_track() 
150
+
151
+    plist = dict['mu_playlist']  
152
+    pl_length = len(dict['mu_playlist'])    
153
+    if pl_length == 0:
154
+        dict['mu_playlist'] = dict['mu_lib']       
155
+         
156
+    #allow controlls while paused or in replay    
157
+    if dict['npause']  == 1 or dict['playback'] == True:
158
+        if dict['yBut'] == False and dict['last_yBut'] == True:
159
+            if dict['mu_stopped'] == 1:
160
+                play_music() 
161
+            else:
162
+                stop_music(sound)  
163
+        if dict['lBump'] == False and dict['last_lBump'] == True:
164
+            previous_track()
165
+        if dict['rBump'] == False and dict['last_rBump'] == True:
166
+            next_track() 

Loading…
Cancel
Save