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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. if 'mp_inited' not in own:
  82. own['mp_inited'] = True
  83. print('Initializing music player')
  84. dict = bge.logic.globalDict
  85. file_list = os.listdir(directory)
  86. dict['mu_lib'] = file_list
  87. dict['mu_playlist'] = file_list
  88. dict['change_track'] = 0
  89. selected = random.choice(file_list)
  90. dict['mu_current_song'] = selected
  91. print('Current Track: ', dict['mu_current_song'])
  92. full_path = os.path.join(directory, selected)
  93. try:
  94. tag = TinyTag.get(full_path)
  95. print('Artist: %s' % tag.artist, 'Track: %s' % tag.title)
  96. dict['mu_artist'] = tag.artist
  97. dict['mu_title'] = tag.title
  98. except:
  99. print("Track has no tag.")
  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'] == 0:
  123. sound.startSound()
  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'] == True 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()
  142. #main()