Shuvit game master repo. http://shuvit.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

music_player.py 5.1KB

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