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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 'mplayback' not in dict:
  30. dict['mplayback'] = 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. dict['music_info'] = info
  71. except:
  72. print("Track has no tag.")
  73. print(dict['mu_title'], 'mu_title')
  74. def next_track():
  75. print("next track")
  76. dict = bge.logic.globalDict
  77. plist = dict['mu_playlist']
  78. cur_song = dict['mu_current_song']
  79. pl_length = len(dict['mu_playlist'])
  80. if pl_length > 1:
  81. try:
  82. plist.remove(cur_song)
  83. except:
  84. pass
  85. if pl_length == 1:
  86. print('rebuilding playlist')
  87. file_list = glob.glob(file_name)
  88. plist = file_list
  89. selected = random.choice(plist)
  90. dict['mu_last_track'] = dict['mu_current_song']
  91. dict['mu_current_song'] = selected
  92. dict['mu_playlist'] = plist
  93. dict['change_track'] = 0
  94. dict['mu_track_time'] = 0
  95. play_music()
  96. def previous_track():
  97. print("previous track")
  98. dict['mu_current_song'] = dict['mu_last_track']
  99. play_music()
  100. def check_status():
  101. sound = bge.logic.getCurrentController().actuators['music_player']
  102. cur_song = dict['mu_current_song']
  103. full_path = os.path.join(directory, dict['mu_current_song'])
  104. sound = bge.logic.getCurrentController().actuators['music_player']
  105. sound.sound = aud.Factory(full_path)
  106. if sound.time < dict['mu_track_time'] and dict['mu_stopped'] == 0 and dict['change_track'] == 0:
  107. print('song over, getting new one')
  108. dict['change_track'] = 1
  109. dict['mu_track_time'] = 0
  110. else:
  111. dict['change_track'] = 0
  112. dict['mu_track_time'] = sound.time
  113. def main(cont):
  114. dict = bge.logic.globalDict
  115. cur_song = dict['mu_current_song']
  116. full_path = os.path.join(directory, dict['mu_current_song'])
  117. sound = bge.logic.getCurrentController().actuators['music_player']
  118. sound.sound = aud.Factory(full_path)
  119. sound.mode = 1
  120. initer(cont)
  121. if dict['music_player'] != 0:
  122. if dict['mu_stopped'] == 1:
  123. play_music()
  124. if dict['change_track'] == 1:
  125. dict['change_track'] = 0
  126. next_track()
  127. plist = dict['mu_playlist']
  128. pl_length = len(dict['mu_playlist'])
  129. if pl_length == 0:
  130. dict['mu_playlist'] = dict['mu_lib']
  131. #allow controlls while paused or in replay
  132. if dict['npause'] == 1 or dict['playback'] == True:
  133. if dict['yBut'] == False and dict['last_yBut'] == True:
  134. if dict['mu_stopped'] == 1:
  135. play_music()
  136. else:
  137. stop_music(sound)
  138. if dict['lBump'] == False and dict['last_lBump'] == True:
  139. previous_track()
  140. if dict['rBump'] == False and dict['last_rBump'] == True:
  141. next_track()