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.

MusicPlayer.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import bge
  2. import os
  3. import aud
  4. import random
  5. import glob
  6. from collections import OrderedDict
  7. from tinytag import TinyTag
  8. """ This is the Music Player as a component """
  9. class MusicPlayer(bge.types.KX_PythonComponent):
  10. args = OrderedDict([
  11. ("Now Playing", ''),
  12. ("Playlist", None),
  13. ("Last Song", ''),
  14. ("Directory", ''),
  15. ("Current Artist", ''),
  16. ("Current Track Title", ''),
  17. ("Track Path", ''),
  18. ("Is Playing", False),
  19. ("Audio Device", None),
  20. ("Audio Factory", None),
  21. ("Keyboard", None)
  22. ])
  23. def start(self, args):
  24. print('Music Player Class Entered')
  25. self.nowPlaying = args['Now Playing']
  26. self.lastSong = args['Last Song']
  27. self.directory = args['Directory']
  28. self.currArtist = args['Current Artist']
  29. self.currTrackTitle = args['Current Track Title']
  30. self.currTrackPath = args['Track Path']
  31. self.isPlaying = args['Is Playing']
  32. self.device = args['Audio Device']
  33. self.factory = args['Audio Factory']
  34. self.keyboard = args['Keyboard']
  35. print('Initilizing...')
  36. self.init()
  37. print('Printing tag')
  38. self.printTag()
  39. def init(self):
  40. # Init the playlist and directory
  41. self.directory = bge.logic.expandPath('//Music')
  42. file_name = self.directory + "\\*.mp3"
  43. file_list = glob.glob(file_name)
  44. print('Directory: ' + self.directory)
  45. print(file_list)
  46. self.playlist = file_list
  47. self.nowPlaying = random.choice(self.playlist)
  48. # Init the audio device
  49. self.device = aud.device()
  50. self.factory = aud.Factory(self.nowPlaying)
  51. # Init the keyboard
  52. self.keyboard = bge.logic.keyboard.events
  53. def update(self):
  54. JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
  55. if self.keyboard.events[bge.events.PKEY] == JUST_ACTIVATED:
  56. if isPlaying:
  57. self.stopMusic()
  58. else:
  59. self.playMusic()
  60. def printTag(self):
  61. print('Current Track: ', self.nowPlaying)
  62. self.currTrackPath = os.path.join(self.directory, self.nowPlaying)
  63. try:
  64. tag = TinyTag.get(self.currTrackPath)
  65. self.currArtist = tag.artist
  66. self.currTrackTitle = tag.title
  67. print('Artist: %s' % self.currArtist,
  68. ' Track: %s' % self.currTrackTitle)
  69. except:
  70. print('Track has no tag')
  71. def playMusic(self):
  72. handle = self.device.play(self.factory)
  73. self.isPlaying = True
  74. def stopMusic(self):
  75. handle = self.device.play(self.factory)
  76. handle.stop(factory)
  77. self.isPlaying = False