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.0KB

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