Guest User

Untitled

a guest
Nov 2nd, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. class VideoLayer(cocos.layer.Layer):
  2.     '''
  3.         Simple Video Layer that sets up when on_enter() is called, tears down with on_exit(),
  4.         and uses a sprite to draw the image with scaling and center.
  5.     '''
  6.     sprite = None
  7.     def __init__(self, video_path):
  8.         '''
  9.             <image> is a loaded pyglet compatible image
  10.         '''
  11.         super(VideoLayer, self).__init__()
  12.         self.video_path = video_path
  13.  
  14.     def on_enter(self):
  15.         '''
  16.             We create the video source from scratch so it doesn't hog memory
  17.         '''
  18.         self.sprite = None
  19.  
  20.         # Set up the media player and video
  21.         self.video = pyglet.media.load(self.video_path)
  22.         player = pyglet.media.Player()
  23.         player.eos_action = player.EOS_LOOP # Could also be EOS_PAUSE or EOS_NEXT
  24.         player.volume = 0
  25.         player.queue(self.video)
  26.         player.play()
  27.         self.player = player
  28.  
  29.     def on_exit(self):
  30.         '''
  31.             Clean up the sprite, video resource, and player
  32.         '''
  33.         self.player.pause()
  34.         del self.player
  35.         del self.sprite
  36.         del self.video
  37.  
  38.     def draw(self):
  39.         '''
  40.             Grab the texture from the video, add to sprite, and draw the sprite.
  41.  
  42.             We do it with a sprite so we can use scaling and other conveniences.
  43.         '''
  44.         #director.window.clear()
  45.         if self.player.playing:
  46.             if self.sprite == None:
  47.                 self.sprite = Sprite(self.player.get_texture())
  48.                 win_width, win_height = director.get_window_size()
  49.                 self.sprite.position = (win_width/2., win_height/2.)
  50.                 self.sprite.scale = (win_width / self.sprite.width)
  51.             else:
  52.                 self.sprite.image = self.player.get_texture()
  53.             # import pdb;pdb.set_trace()
  54.             self.sprite.draw()
Advertisement
Add Comment
Please, Sign In to add comment