Skip to content

Commit

Permalink
add html5 media example
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Leighton committed Mar 27, 2012
1 parent d89ac67 commit a9bffca
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 25 deletions.
24 changes: 24 additions & 0 deletions examples/media/Media.py
@@ -0,0 +1,24 @@
import pyjd # this is dummy in pyjs.
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.media.Video import Video
from pyjamas.media.Audio import Audio
from pyjamas import Window

if __name__ == '__main__':
pyjd.setup("public/Media.html?fred=foo#me")
v = Video(Width="640px", Height="480px",
StyleName="vteststyle",
Autoplay=True,
Controls=True,
src="http://127.0.0.1/home/kiss.the.girl.mp4")

a = Audio(Width="640px", Height="50px",
StyleName="ateststyle",
Autoplay=True,
Controls=True,
src="http://127.0.0.1/home/Lry_Crane_Copy.mp3")

RootPanel().add(a)
#RootPanel().add(v)

pyjd.run()
53 changes: 53 additions & 0 deletions examples/media/__main__.py
@@ -0,0 +1,53 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


TARGETS = [
'Media.py',
]


PACKAGE = {
'title': 'helloworld',
'desc': 'Port of GWT hello world example',
}


def setup(targets):
'''Setup example for translation, MUST call util.setup(targets).'''
util.setup(targets)


def translate():
'''Translate example, MUST call util.translate().'''
util.translate()


def install(package):
'''Install and cleanup example module. MUST call util.install(package)'''
util.install(package)


##---------------------------------------##
# --------- (-: DO NOT EDIT :-) --------- #
##---------------------------------------##


import sys
import os


examples = head = os.path.abspath(os.path.dirname(__file__))
while os.path.split(examples)[1].lower() != 'examples':
examples = os.path.split(examples)[0]
if not examples:
raise ValueError("Cannot determine examples directory")
sys.path.insert(0, os.path.join(examples))
from _examples import util
sys.path.pop(0)

util.init(head)

setup(TARGETS)
translate()
install(PACKAGE)
7 changes: 7 additions & 0 deletions examples/media/build.sh
@@ -0,0 +1,7 @@
#!/bin/sh
# you will need to read the top level README, and run boostrap.py
# and buildout in order to make pyjsbuild

options="$*"
#if [ -z $options ] ; then options="-O";fi
../../bin/pyjsbuild --print-statements $options Media
7 changes: 7 additions & 0 deletions examples/media/public/Media.css
@@ -0,0 +1,7 @@
.teststyle {
font-size: 200%;
margin: 20px;
padding: 20px;
background-color: #00ff00;
}

12 changes: 12 additions & 0 deletions examples/media/public/Media.html
@@ -0,0 +1,12 @@
<html>
<!-- auto-generated html - You should consider editing and adapting this
to suit your requirements. No doctype used here to force quirks mode; see
wiki for details: http://pyjs.org/wiki/csshellandhowtodealwithit/
-->
<head>
<link rel="stylesheet" href="Media.css">
<title>Media (Pyjamas Auto-Generated HTML file)</title>
</head>
<body style="background-color:white">
</body>
</html>
3 changes: 2 additions & 1 deletion library/pyjamas/media/Audio.py
Expand Up @@ -22,9 +22,10 @@
"""
class Audio(Media):

def __init__(self, src=None):
def __init__(self, src=None, **kwargs):
self.setElement(DOM.createElement("audio"))
if src:
self.setSrc(src)

Media.__init__(self, **kwargs)

45 changes: 21 additions & 24 deletions library/pyjamas/media/Media.py
Expand Up @@ -15,9 +15,8 @@
"""


from pyjamas import Event
from pyjamas.ui import Event
from pyjamas import DOM
from pyjamas import EventListener
from pyjamas.ui.Widget import Widget

class Media(Widget):
Expand All @@ -33,6 +32,12 @@ class Media(Widget):
HasVolumeChangeHandlers, HasWaitingHandlers, HasAllMouseHandlers,
HasClickHandlers"""

def __init__(self, **kwargs):
self.mediaEventsToSink = 0
self.mediaEventsInitialized = False

Widget.__init__(self, **kwargs)

def setSrc(self, src):
DOM.setAttribute(self.getElement(), 'src', src)

Expand Down Expand Up @@ -140,10 +145,10 @@ def setLoop(self, loop):
* @param controls Whether the browser should show playback controls
"""
def setControls(self, controls):
DOM.setBooleanAttr(self.getElement(), "controls", controls)
DOM.setBooleanAttribute(self.getElement(), "controls", controls)

def hasControls(self):
DOM.getBooleanAttr(self.getElement(), "controls")
DOM.getBooleanAttribute(self.getElement(), "controls")

def isMuted(self):
return self.getElement().muted
Expand Down Expand Up @@ -194,14 +199,12 @@ def addCanPlayHandler(self, handler):
* @param handler the {@link CanPlayThroughHandler} to be called
"""

HandlerRegistration addCanPlayThroughHandler(
CanPlayThroughHandler handler) {
def addCanPlayThroughHandler(self, handler):
return self.addMediaEventHandler(handler, CanPlayThroughEvent.getType())



HandlerRegistration addDurationChangeHandler(
DurationChangeHandler handler) {
def addDurationChangeHandle(self, handler):
return self.addMediaEventHandler(handler, DurationChangeEvent.getType())


Expand Down Expand Up @@ -270,8 +273,7 @@ def addLoadedDataHandler(self, handler):
* @param handler the {@link LoadedMetadataHandler} to be called
"""

HandlerRegistration addLoadedMetadataHandler(
LoadedMetadataHandler handler) {
def addLoadedMetadataHandler(self, handler):
return self.addMediaEventHandler(handler, LoadedMetadataEvent.getType())


Expand Down Expand Up @@ -402,11 +404,9 @@ def addWaitingHandler(self, handler):
return self.addMediaEventHandler(handler, WaitingEvent.getType())


int mediaEventsToSink = 0

def addMediaEventHandler(self, handler, etype):
assert handler is not None : "handler must not be None"
assert etype is not None : "type must not be None"
assert handler is not None, "handler must not be None"
assert etype is not None, "type must not be None"
self.maybeInitMediaEvents()
self.sinkMediaEvents(mediaEventGetTypeInt(etype.getName()))
return addHandler(handler, etype)
Expand Down Expand Up @@ -446,10 +446,10 @@ def mediaEventGetTypeInt(self, eventType):


def sinkMediaEvents(self, eventBitsToAdd):
if isOrWasAttached():
nativeSinkMediaEvents(self.getElement(), eventBitsToAdd)
else:
mediaEventsToSink |= eventBitsToAdd
if self.isOrWasAttached():
self.nativeSinkMediaEvents(self.getElement(), eventBitsToAdd)
else:
self.mediaEventsToSink |= eventBitsToAdd



Expand All @@ -459,13 +459,12 @@ def sinkMediaEvents(self, eventBitsToAdd):
* element.
"""
def doAttachChildren(self):
int bitsToAdd = mediaEventsToSink
mediaEventsToSink = -1
bitsToAdd = self.mediaEventsToSink
self.mediaEventsToSink = -1
if bitsToAdd > 0:
nativeSinkMediaEvents(self.getElement(), bitsToAdd)
self.nativeSinkMediaEvents(self.getElement(), bitsToAdd)



def nativeSinkMediaEvents(self, elem, bits):
JS("""
var chMask = (elem.__mediaEventBits || 0) ^ bits;
Expand Down Expand Up @@ -576,8 +575,6 @@ def addClickHandler(self, handler):
return addDomHandler(handler, ClickEvent.getType())


boolean mediaEventsInitialized = False

def maybeInitMediaEvents(self):
if not mediaEventsInitialized:
initMediaEvents()
Expand Down

0 comments on commit a9bffca

Please sign in to comment.