Hi,

I just start playing around with cocos2d and made this video player
for no other reason than fun.
To start the player do in a shell:
$ python video.py video1 video2

It will play one video at a time, ut you can jump between them
pressing any key.
There are random transitions when jumping videos.

It will be better to have avbin available on pyglet.

I post the code here (just tested on Ubuntu 9.04).
Could be handy for someone.

import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

import cocos
from cocos.layer import Layer
from cocos.scene import Scene
from cocos.actions import *
#from cocos.batch import *
from cocos.scenes.transitions import *
import cocos.cocosnode as cocosnode
import pyglet
from pyglet.gl import *
import random

class VideoElement(cocosnode.CocosNode):
    def __init__(self, position=(0,0), **kwargs):
        super(VideoElement, self).__init__()
        self.position = position
        self.args = []
        self.kwargs = kwargs
        self.group = None
        self.batch = None

        self.batch = pyglet.graphics.Batch()
        self.create_element()

    def create_element(self):
        self.element = self.klass() #group=self.group,
batch=self.batch, **self.kwargs)

    def queue(self, source):
        self.element.queue(source)

    def get_texture(self):
        return self.element.get_texture()

    # cannot use "def pause" here. CocosNode already has a pause
method
    def stop(self):
        self.element.pause()

    def play(self):
        self.element.play()

    def draw(self):
        glPushMatrix()
        self.transform()
        im = self.element.get_texture()
        try:
            im.blit(0, 0)
        except AttributeError:
            self.stop()
        glPopMatrix()

    def _get_opacity(self):
        return self.element.color[3]
    def _set_opacity(self, value):
        self.element.color = tuple(self.element.color[:3]) + (int
(value),)
    opacity = property(_get_opacity, _set_opacity)

class Player(VideoElement):
    klass = pyglet.media.Player

class Video(Layer):
    def __init__(self, source):
        super( Video, self ).__init__()

        self.player = player = Player()
        player.queue(source)
        player.position = 0,0

        #scale = ScaleBy(.2, duration=1)
        #player.do( scale + Reverse(scale))

        self.add( player )

trans = [
CornerMoveTransition,
EnvelopeTransition,
FadeBLTransition,
FadeDownTransition,
FadeTRTransition,
FadeTransition,
FadeUpTransition,
FlipAngular3DTransition,
FlipX3DTransition,
FlipY3DTransition,
JumpZoomTransition,
MoveInBTransition,
MoveInLTransition,
MoveInRTransition,
MoveInTTransition,
RotoZoomTransition,
ShrinkGrowTransition,
ShuffleTransition,
SlideInBTransition,
SlideInLTransition,
SlideInRTransition,
SlideInTTransition,
SplitColsTransition,
SplitRowsTransition,
#TransitionScene,
TurnOffTilesTransition,
#ZoomTransition,
]
class ControlLayer(Layer):
    is_event_handler = True     #: enable pyglet's events
    flag = True

    def on_key_press(self, k, m):
        idx = random.randint(0, len(trans)-1)
        print 'trans:', `trans[idx]`
        if self.flag:
            video2.player.play()
            #cocos.director.director.replace( FlipX3DTransition
(controlScene2, .3, controlScene1) )
            #cocos.director.director.replace( SlideInLTransition
(controlScene2, .3, controlScene1) )
            #cocos.director.director.replace( CornerMoveTransition
(controlScene2, .3, controlScene1) )
            #cocos.director.director.replace( EnvelopeTransition
(controlScene2, .3, controlScene1) )
            cocos.director.director.replace( trans[idx]
(controlScene2, .3, controlScene1) )
            video1.player.stop()
        else:
            video1.player.play()
            cocos.director.director.replace( trans[idx]
(controlScene1, .3, controlScene2) )
            video2.player.stop()
        self.flag = not self.flag


if __name__ == "__main__":

    source1 = pyglet.media.load(sys.argv[1])
    format1 = source1.video_format
    if not format1:
        print 'No video track in source1.'
        sys.exit(1)

    source2 = pyglet.media.load(sys.argv[2])
    format2 = source2.video_format
    if not format2:
        print 'No video track in source2.'
        sys.exit(1)

    width = min(format1.width, format2.width)
    height = min(format1.height, format2.height)

    # director init takes the same arguments as pyglet.window
    cocos.director.director.init(width=width, height=height,
resizable=True)
    #cocos.director.director.set_depth_test()

    # Videos can have different dimentions. Code below will
"normalize" them
    scale = min(float(width)/format1.width, float(height)/
format1.height)
    video1 = Video (source1)  # Video 1
    video1.scale = scale
    ax = abs((format1.width*scale-width)/2)
    ay = abs((format1.height*scale-height)/2)
    video1.anchor_x = ax
    video1.anchor_y = ay
    video1.player.play()
    scene1 = Scene (video1)  # Scene video 1
    scene1.do( ScaleBy(width/format1.width, duration=.3) )

    dx = float(width)/format2.width
    dy = float(height)/format2.height
    scale = min(dx, dy)
    video2 = Video (source2)  # Video 2
    video2.scale = scale
    ax = abs((format2.width*scale-width)/2)
    ay = abs((format2.height*scale-height)/2)
    video2.anchor_x = ax
    video2.anchor_y = ay
    scene2 = Scene (video2) # Scene video 2
    print(width,format2.width)
    #scene2.do( ScaleTo(scale, duration=.3) )

    control = ControlLayer()
    controlScene1 = Scene( scene1, control)
    controlScene2 = Scene( scene2, control)

    # And now, start the application, starting with scene1
    cocos.director.director.run (controlScene1)


Regars,
Ricardo
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"cocos2d discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cocos-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to