Shuvit game release repo.
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.8KB

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