#79 Fixing issue #70 limit songs to mp3 files only

Sammanfogat
shuvit sammanfogade 3 incheckningar från shuvit/shuvit:Music-Player_fix in i dev 5 år sedan

Binär
Test/Music/Divineinfinite - Bang This.mp3 Visa fil


Binär
Test/Music/Just 1 Soul - Growing.mp3 Visa fil


+ 1
- 0
Test/Music/Test.txt Visa fil

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

Binär
Test/Music/alien-spaceship_daniel_simion.wav Visa fil


+ 43
- 50
music_player.py Visa fil

@@ -5,27 +5,54 @@ import bge
5 5
 import os
6 6
 import aud
7 7
 import random
8
+import glob
8 9
 from tinytag import TinyTag
9 10
 
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
-
11
+# dict
15 12
 dict = bge.logic.globalDict
16 13
 dict['mu_current_song'] = ''
17
-dict['mu_stopped'] = 0
18
-dict['mu_playlist'] = ''
19
-dict['mu_lib'] = ''
14
+dict['mu_stopped'] = 1
20 15
 dict['change_track'] = 0
21 16
 dict['mu_track_time'] = 0
22
-#dict['mu_last_track'] = ''
23
-#dict['mu_artist'] = ''
24
-#dict['mu_title'] = ''
25 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
26 28
 keyboard = bge.logic.keyboard
27 29
 JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
28 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
+            
29 56
 def stop_music(sound):
30 57
 
31 58
     print("Stop music")    
@@ -45,18 +72,17 @@ def play_music():
45 72
     dict['change_track'] = 0
46 73
     dict['mu_track_time'] = 0 
47 74
     dict['music_player'] = 1  
48
-
49 75
     try:
50 76
         tag = TinyTag.get(full_path)
51 77
         print('Playing: ', 'Artist: %s' % tag.artist, 'Track: %s' % tag.title)
52 78
         dict['mu_artist'] = tag.artist
53 79
         dict['mu_title'] = tag.title
54 80
         info = ('Music > ', 'Artist: %s' % tag.artist, 'Track: %s' % tag.title)
55
-        print(info)
56 81
         dict['music_info'] = info
57 82
     except:
58 83
         print("Track has no tag.") 
59 84
     print(dict['mu_title'], 'mu_title')
85
+    
60 86
 def next_track():
61 87
 
62 88
     print("next track")
@@ -73,7 +99,7 @@ def next_track():
73 99
 
74 100
     if pl_length == 1:    
75 101
         print('rebuilding playlist')
76
-        file_list = os.listdir(directory) 
102
+        file_list = glob.glob(file_name)
77 103
         plist = file_list
78 104
 
79 105
     selected = random.choice(plist)
@@ -84,43 +110,12 @@ def next_track():
84 110
     dict['mu_track_time'] = 0
85 111
     play_music()
86 112
 
87
-    
88
-
89
-    #dict['music_info'] = info    
90
-
91 113
 def previous_track():
92 114
 
93 115
     print("previous track") 
94 116
     dict['mu_current_song'] = dict['mu_last_track']
95 117
     play_music()   
96 118
 
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 119
 def check_status():
125 120
 
126 121
     sound = bge.logic.getCurrentController().actuators['music_player']
@@ -147,8 +142,8 @@ def main(cont):
147 142
     sound.mode = 1
148 143
     initer(cont)
149 144
     if dict['music_player'] != 0:
150
-        if dict['mu_stopped'] == 0:        
151
-            sound.startSound()
145
+        if dict['mu_stopped'] == 1:        
146
+            play_music()
152 147
         if dict['change_track'] == 1:
153 148
             dict['change_track'] = 0
154 149
             next_track() 
@@ -157,6 +152,7 @@ def main(cont):
157 152
     pl_length = len(dict['mu_playlist'])    
158 153
     if pl_length == 0:
159 154
         dict['mu_playlist'] = dict['mu_lib']       
155
+         
160 156
     #allow controlls while paused or in replay    
161 157
     if dict['npause']  == 1 or dict['playback'] == True:
162 158
         if dict['yBut'] == False and dict['last_yBut'] == True:
@@ -168,6 +164,3 @@ def main(cont):
168 164
             previous_track()
169 165
         if dict['rBump'] == False and dict['last_rBump'] == True:
170 166
             next_track() 
171
-            
172
-    
173
-#main()        

Loading…
Avbryt
Spara