12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import bge
- import os
- import aud
- import random
- import glob
- from collections import OrderedDict
- from tinytag import TinyTag
-
- """ This is the Music Player as a component """
- class MusicPlayer(bge.types.KX_PythonComponent):
-
- args = OrderedDict([
- ("Now Playing", ''),
- ("Playlist", None),
- ("Last Song", ''),
- ("Directory", ''),
- ("Current Artist", ''),
- ("Current Track Title", ''),
- ("Track Path", ''),
- ("Is Playing", False),
- ("Audio Device", None),
- ("Audio Factory", None),
- ("Keyboard", None)
- ])
-
- def start(self, args):
- print('Music Player Class Entered')
- self.nowPlaying = args['Now Playing']
- self.lastSong = args['Last Song']
- self.directory = args['Directory']
- self.currArtist = args['Current Artist']
- self.currTrackTitle = args['Current Track Title']
- self.currTrackPath = args['Track Path']
- self.isPlaying = args['Is Playing']
- self.device = args['Audio Device']
- self.factory = args['Audio Factory']
- self.keyboard = args['Keyboard']
-
- print('Initilizing...')
- self.init()
-
- print('Printing tag')
- self.printTag()
-
- def init(self):
- # Init the playlist and directory
- self.directory = bge.logic.expandPath('//Music')
- file_name = self.directory + "\\*.mp3"
- file_list = glob.glob(file_name)
- print('Directory: ' + self.directory)
- print(file_list)
- self.playlist = file_list
- self.nowPlaying = random.choice(self.playlist)
-
- # Init the audio device
- self.device = aud.device()
- self.factory = aud.Factory(self.nowPlaying)
-
- # Init the keyboard
- self.keyboard = bge.logic.keyboard.events
-
- def update(self):
- JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
-
- if self.keyboard.events[bge.events.PKEY] == JUST_ACTIVATED:
- if isPlaying:
- self.stopMusic()
- else:
- self.playMusic()
-
- def printTag(self):
- print('Current Track: ', self.nowPlaying)
- self.currTrackPath = os.path.join(self.directory, self.nowPlaying)
-
- try:
- tag = TinyTag.get(self.currTrackPath)
- self.currArtist = tag.artist
- self.currTrackTitle = tag.title
- print('Artist: %s' % self.currArtist,
- ' Track: %s' % self.currTrackTitle)
- except:
- print('Track has no tag')
-
- def playMusic(self):
- handle = self.device.play(self.factory)
- self.isPlaying = True
-
- def stopMusic(self):
- handle = self.device.play(self.factory)
- handle.stop(factory)
- self.isPlaying = False
|