Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class VideoLayer(cocos.layer.Layer):
- '''
- Simple Video Layer that sets up when on_enter() is called, tears down with on_exit(),
- and uses a sprite to draw the image with scaling and center.
- '''
- sprite = None
- def __init__(self, video_path):
- '''
- <image> is a loaded pyglet compatible image
- '''
- super(VideoLayer, self).__init__()
- self.video_path = video_path
- def on_enter(self):
- '''
- We create the video source from scratch so it doesn't hog memory
- '''
- self.sprite = None
- # Set up the media player and video
- self.video = pyglet.media.load(self.video_path)
- player = pyglet.media.Player()
- player.eos_action = player.EOS_LOOP # Could also be EOS_PAUSE or EOS_NEXT
- player.volume = 0
- player.queue(self.video)
- player.play()
- self.player = player
- def on_exit(self):
- '''
- Clean up the sprite, video resource, and player
- '''
- self.player.pause()
- del self.player
- del self.sprite
- del self.video
- def draw(self):
- '''
- Grab the texture from the video, add to sprite, and draw the sprite.
- We do it with a sprite so we can use scaling and other conveniences.
- '''
- #director.window.clear()
- if self.player.playing:
- if self.sprite == None:
- self.sprite = Sprite(self.player.get_texture())
- win_width, win_height = director.get_window_size()
- self.sprite.position = (win_width/2., win_height/2.)
- self.sprite.scale = (win_width / self.sprite.width)
- else:
- self.sprite.image = self.player.get_texture()
- # import pdb;pdb.set_trace()
- self.sprite.draw()
Advertisement
Add Comment
Please, Sign In to add comment