Skip to content

Instantly share code, notes, and snippets.

@bergman
Created August 19, 2010 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bergman/537653 to your computer and use it in GitHub Desktop.
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.
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 = this.@MyVideoPlayer.Player::video.getElement()();
$wnd.addEventListener("unload", function() { instance.@MyVideoPlayer.Player::onUnload()(); });
v.addEventListener("click", function() { instance.@MyVideoPlayer.Player::onClick()(); });
v.addEventListener("touchstart", function() { instance.@MyVideoPlayer.Player::onClick()(); });
v.addEventListener("ended", function() { instance.@MyVideoPlayer.Player::onEnded()(); });
v.addEventListener("play", function() { instance.@MyVideoPlayer.Player::onPlay()(); });
v.addEventListener("pause", function() { instance.@MyVideoPlayer.Player::onPause()(); });
v.addEventListener("timeupdate", function() { instance.@MyVideoPlayer.Player::onTimeupdate()(); });
v.addEventListener("loadedmetadata", function() { instance.@MyVideoPlayer.Player::onLoadedmetadata()(); });
v.addEventListener("durationchange", function() { instance.@MyVideoPlayer.Player::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);
};
}-*/;
}
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