Farmerjoe12 5 роки тому
джерело
коміт
0127ab478c

BIN
Test/Divineinfinite - Bang This.mp3 Переглянути файл


BIN
Test/Just 1 Soul - Growing.mp3 Переглянути файл


+ 1
- 0
Test/Test.txt Переглянути файл

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

BIN
Test/alien-spaceship_daniel_simion.wav Переглянути файл


+ 168
- 0
Test/music_player.py Переглянути файл

@@ -0,0 +1,168 @@
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 'playback' not in dict:
35
+        dict['playback'] = 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
+
76
+    try:
77
+        tag = TinyTag.get(full_path)
78
+        print('Playing: ', 'Artist: %s' % tag.artist, 'Track: %s' % tag.title)
79
+        dict['mu_artist'] = tag.artist
80
+        dict['mu_title'] = tag.title
81
+        info = ('Music > ', 'Artist: %s' % tag.artist, 'Track: %s' % tag.title)
82
+        print(info)
83
+        dict['music_info'] = info
84
+    except:
85
+        print("Track has no tag.") 
86
+    print(dict['mu_title'], 'mu_title')
87
+    
88
+def next_track():
89
+
90
+    print("next track")
91
+    dict = bge.logic.globalDict
92
+    plist = dict['mu_playlist']
93
+    cur_song = dict['mu_current_song']
94
+    pl_length = len(dict['mu_playlist'])    
95
+
96
+    if pl_length > 1:
97
+        try:
98
+            plist.remove(cur_song)
99
+        except:
100
+            pass    
101
+
102
+    if pl_length == 1:    
103
+        print('rebuilding playlist')
104
+        file_list = glob.glob(file_name)
105
+        plist = file_list
106
+
107
+    selected = random.choice(plist)
108
+    dict['mu_last_track'] = dict['mu_current_song']
109
+    dict['mu_current_song'] = selected
110
+    dict['mu_playlist'] = plist
111
+    dict['change_track'] = 0
112
+    dict['mu_track_time'] = 0
113
+    play_music()
114
+
115
+def previous_track():
116
+
117
+    print("previous track") 
118
+    dict['mu_current_song'] = dict['mu_last_track']
119
+    play_music()   
120
+
121
+def check_status():
122
+
123
+    sound = bge.logic.getCurrentController().actuators['music_player']
124
+    cur_song = dict['mu_current_song']
125
+    full_path = os.path.join(directory, dict['mu_current_song'])
126
+    sound = bge.logic.getCurrentController().actuators['music_player']
127
+    sound.sound = aud.Factory(full_path)  
128
+
129
+    if sound.time < dict['mu_track_time'] and dict['mu_stopped'] == 0 and dict['change_track'] == 0:
130
+        print('song over, getting new one')
131
+        dict['change_track'] = 1
132
+        dict['mu_track_time'] = 0
133
+    else:
134
+        dict['change_track'] = 0
135
+    dict['mu_track_time'] = sound.time    
136
+
137
+def main(cont):
138
+
139
+    dict = bge.logic.globalDict        
140
+    cur_song = dict['mu_current_song']    
141
+    full_path = os.path.join(directory, dict['mu_current_song'])
142
+    sound = bge.logic.getCurrentController().actuators['music_player']
143
+    sound.sound = aud.Factory(full_path)
144
+    sound.mode = 1
145
+    initer(cont)
146
+    if dict['music_player'] != 0:
147
+        if dict['mu_stopped'] == 0:        
148
+            sound.startSound()
149
+        if dict['change_track'] == 1:
150
+            dict['change_track'] = 0
151
+            next_track() 
152
+
153
+    plist = dict['mu_playlist']  
154
+    pl_length = len(dict['mu_playlist'])    
155
+    if pl_length == 0:
156
+        dict['mu_playlist'] = dict['mu_lib']       
157
+         
158
+    #allow controlls while paused or in replay    
159
+    if dict['npause']  == 1 or dict['playback'] == True:
160
+        if dict['yBut'] == False and dict['last_yBut'] == True:
161
+            if dict['mu_stopped'] == 1:
162
+                play_music()    
163
+            else:
164
+                stop_music(sound)  
165
+        if dict['lBump'] == False and dict['last_lBump'] == True:
166
+            previous_track()
167
+        if dict['rBump'] == False and dict['last_rBump'] == True:
168
+            next_track() 

Завантаження…
Відмінити
Зберегти