Created
August 19, 2010 11:30
-
-
Save bergman/537653 to your computer and use it in GitHub Desktop.
A quick example of how to set up events for a video element that is already in the DOM and thus not created from within GWT.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyVideoPlayer; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Map; | |
import com.google.code.gwt.html5.media.client.VideoElement; | |
import com.google.gwt.core.client.EntryPoint; | |
import com.google.gwt.core.client.JavaScriptObject; | |
import com.google.gwt.dom.client.Element; | |
class Player implements EntryPoint { | |
private VideoWrapper video; | |
@Override | |
public void onModuleLoad() { | |
exposeJavaScriptAPI(); | |
} | |
public Player(JavaScriptObject element) { | |
video = VideoWrapper.wrap(element.<VideoElement> cast()); | |
bindEvents(); | |
} | |
private native void bindEvents() /*-{ | |
var instance = this; | |
var v = [email protected]::video.getElement()(); | |
$wnd.addEventListener("unload", function() { [email protected]::onUnload()(); }); | |
v.addEventListener("click", function() { [email protected]::onClick()(); }); | |
v.addEventListener("touchstart", function() { [email protected]::onClick()(); }); | |
v.addEventListener("ended", function() { [email protected]::onEnded()(); }); | |
v.addEventListener("play", function() { [email protected]::onPlay()(); }); | |
v.addEventListener("pause", function() { [email protected]::onPause()(); }); | |
v.addEventListener("timeupdate", function() { [email protected]::onTimeupdate()(); }); | |
v.addEventListener("loadedmetadata", function() { [email protected]::onLoadedmetadata()(); }); | |
v.addEventListener("durationchange", function() { [email protected]::onDurationChange()(); }); | |
}-*/; | |
@SuppressWarnings("unused") | |
private void onClick() { | |
} | |
@SuppressWarnings("unused") | |
private void onDurationChange() { | |
} | |
@SuppressWarnings("unused") | |
private void onEnded() { | |
} | |
@SuppressWarnings("unused") | |
private void onLoadedmetadata() { | |
} | |
@SuppressWarnings("unused") | |
private void onPause() { | |
} | |
@SuppressWarnings("unused") | |
private void onPlay() { | |
} | |
@SuppressWarnings("unused") | |
private void onTimeupdate() { | |
} | |
@SuppressWarnings("unused") | |
private void onUnload() { | |
} | |
protected static native void exposeJavaScriptAPI() /*-{ | |
$wnd.Player = function(element) { | |
this.instance = @MyVideoPlayer.Player::new(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)(element); | |
}; | |
}-*/; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.code.gwt.html5.media.client.Media; | |
import com.google.code.gwt.html5.media.client.VideoElement; | |
import com.google.gwt.user.client.ui.RootPanel; | |
class VideoWrapper extends Media { | |
private VideoWrapper(VideoElement e) { | |
setElement(e); | |
} | |
/* | |
* Needed in GWT 2.0.3 because it kills unknown events. In 2.1 this isn't | |
* the case. Any unknown events are sent along. | |
*/ | |
@Override | |
public void onBrowserEvent(Event event) { | |
DomEvent.fireNativeEvent(event, this, this.getElement()); | |
} | |
protected static VideoWrapper wrap(VideoElement e) { | |
assert Document.get().getBody().isOrHasChild(e); | |
VideoWrapper v = new VideoWrapper(e); | |
v.maybeInitMediaEvents(); | |
// The two lines below seems to be good practice to avoid memory leaks... | |
v.onAttach(); | |
RootPanel.detachOnWindowClose(v); | |
return v; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment