import pyglet player_window = pyglet.window.Window(800, 480) player = pyglet.media.Player() song=pyglet.media.load('thesong.mp3') player.queue(song) player.play() pyglet.app.run()
Pyglet's player class has a lot of built-in functionality that should streamline the development of this media player (which is good because I have to do it in 2 weeks!). Currently my prototype has the graphics positioned and updating. Here is a screenshot:
Here is the full code so far:
#--------------------------------------------------------------------- # piamp prototype #--------------------------------------------------------------------- import os import pyglet dir = pyglet.resource.get_settings_path('Piamp') if not os.path.exists(dir): os.makedirs(dir) configFileName = os.path.join(dir, 'settings.cfg') pyglet.resource.path = ['resources'] pyglet.resource.reindex() batch = pyglet.graphics.Batch() class Piamp(pyglet.window.Window): def __init__(self): super(Piamp, self).__init__() self.set_size(800, 480) configFile = open(configFileName, 'wt') self.load_graphics() self.canPlay = True self.canMute = True self.mainMenuVisible = True self.frameCount = 0 player = pyglet.media.Player() player.volume = 0.5 def load_graphics(self): #Background: self.img_background = pyglet.resource.image('background.png') self.img_background.anchor_x = self.img_background.width/2 self.img_background.anchor_y = self.img_background.height/2 #Main Menu Buttons: self.img_movies = pyglet.resource.image('movies.png') self.img_movies.anchor_x = self.img_movies.width/2 self.img_movies.anchor_y = self.img_movies.height/2 self.img_music = pyglet.resource.image('music.png') self.img_music.anchor_x = self.img_music.width/2 self.img_music.anchor_y = self.img_music.height/2 self.img_shutdown = pyglet.resource.image('shutdown.png') self.img_shutdown.anchor_x = self.img_shutdown.width/2 self.img_shutdown.anchor_y = self.img_shutdown.height/2 self.img_reboot = pyglet.resource.image('reboot.png') self.img_reboot.anchor_x = self.img_reboot.width/2 self.img_reboot.anchor_y = self.img_reboot.height/2 self.img_eq = pyglet.resource.image('equalizer.png') self.img_eq.anchor_x = self.img_eq.width/2 self.img_eq.anchor_y = self.img_eq.height/2 #Player Control Buttons: self.img_mute = pyglet.resource.image('mute.png') self.img_mute.anchor_x = self.img_mute.width/2 self.img_mute.anchor_y = self.img_mute.height/2 self.img_unmute = pyglet.resource.image('unmute.png') self.img_unmute.anchor_x = self.img_unmute.width/2 self.img_unmute.anchor_y = self.img_unmute.height/2 self.img_prev = pyglet.resource.image('previous.png') self.img_prev.anchor_x = self.img_prev.width/2 self.img_prev.anchor_y = self.img_prev.height/2 self.img_rewind = pyglet.resource.image('rewind.png') self.img_rewind.anchor_x = self.img_rewind.width/2 self.img_rewind.anchor_y = self.img_rewind.height/2 self.img_play = pyglet.resource.image('play.png') self.img_play.anchor_x = self.img_play.width/2 self.img_play.anchor_y = self.img_play.height/2 self.img_pause = pyglet.resource.image('pause.png') self.img_pause.anchor_x = self.img_pause.width/2 self.img_pause.anchor_y = self.img_pause.height/2 self.img_ff = pyglet.resource.image('ff.png') self.img_ff.anchor_x = self.img_ff.width/2 self.img_ff.anchor_y = self.img_ff.height/2 self.img_next = pyglet.resource.image('next.png') self.img_next.anchor_x = self.img_next.width/2 self.img_next.anchor_y = self.img_next.height/2 def update_sprites(self): #print("Entering update_sprites function...") self.backgroundSprite = pyglet.sprite.Sprite(self.img_background, x=400, y=240, batch=batch) if(self.mainMenuVisible == True): self.mainMenuSprites = [pyglet.sprite.Sprite(self.img_movies, x=280, y=315, batch=batch), pyglet.sprite.Sprite(self.img_music, x=525, y=315, batch=batch), pyglet.sprite.Sprite(self.img_shutdown, x=60, y=425, batch=batch), pyglet.sprite.Sprite(self.img_reboot, x=60, y=305, batch=batch), pyglet.sprite.Sprite(self.img_eq, x=60, y=185, batch=batch)] if(self.canPlay == True and self.canMute == True): self.controlSprites = [pyglet.sprite.Sprite(self.img_mute, x=50, y=35, batch=batch), pyglet.sprite.Sprite(self.img_prev, x=208, y=35, batch=batch), pyglet.sprite.Sprite(self.img_rewind, x=304, y=35, batch=batch), pyglet.sprite.Sprite(self.img_play, x=400, y=35, batch=batch), pyglet.sprite.Sprite(self.img_ff, x=496, y=35, batch=batch), pyglet.sprite.Sprite(self.img_next, x=592, y=35, batch=batch)] if(self.canPlay == False and self.canMute == True): self.controlSprites = [pyglet.sprite.Sprite(self.img_mute, x=50, y=360, batch=batch), pyglet.sprite.Sprite(self.img_prev, x=208, y=360, batch=batch), pyglet.sprite.Sprite(self.img_rewind, x=304, y=360, batch=batch), pyglet.sprite.Sprite(self.img_pause, x=400, y=360, batch=batch), pyglet.sprite.Sprite(self.img_ff, x=496, y=360, batch=batch), pyglet.sprite.Sprite(self.img_next, x=592, y=360, batch=batch)] if(self.canPlay == False and self.canMute == False): self.controlSprites = [pyglet.sprite.Sprite(self.img_unmute, x=50, y=360, batch=batch), pyglet.sprite.Sprite(self.img_prev, x=208, y=360, batch=batch), pyglet.sprite.Sprite(self.img_rewind, x=304, y=360, batch=batch), pyglet.sprite.Sprite(self.img_pause, x=400, y=360, batch=batch), pyglet.sprite.Sprite(self.img_ff, x=496, y=360, batch=batch), pyglet.sprite.Sprite(self.img_next, x=592, y=360, batch=batch)] if(self.canPlay == True and self.canMute == False): self.controlSprites = [pyglet.sprite.Sprite(self.img_unmute, x=50, y=360, batch=batch), pyglet.sprite.Sprite(self.img_prev, x=208, y=360, batch=batch), pyglet.sprite.Sprite(self.img_rewind, x=304, y=360, batch=batch), pyglet.sprite.Sprite(self.img_play, x=400, y=360, batch=batch), pyglet.sprite.Sprite(self.img_ff, x=496, y=360, batch=batch), pyglet.sprite.Sprite(self.img_next, x=592, y=360, batch=batch)] def on_draw(self): self.clear() if(self.frameCount%10 == 0): #print("Resetting frame count") self.frameCount = 0 self.update_sprites() batch.draw() self.frameCount += 1 def check_for_button(self, x, y): #Check all button coordinates against x and y, return True if a button was hit or False if not pass def on_mouse_press(x, y, button, modifiers): if (check_for_button(x, y)): pass pass def on_mouse_release(x, y, button, modifiers): pass def on_mouse_drag(x, y, dx, dy, buttons, modifiers): pass if __name__ == '__main__': window = Piamp() #window.set_mouse_visible(False) pyglet.app.run()
Keep in mind this code is still quite raw. After I've got the media player in a fairly stable setting, I'll walk through the entire code file step by step and explain what is going on.
hi, which part of the code is used to play movies?, Thank you
ReplyDeleteHello,
ReplyDeleteAt this post's stage of development, video payback was not yet implemented, please review the final post of this project and download the final project file to review the code with video playback implemented. That said, this project was done more than a few years ago and I haven't updated it in as long - I'm not sure about any library incompatibilities that may have been introduced as those libraries were updated and this project wasn't. Thanks!