Search code examples
gwtjsni

Using phonegap audio api in gwt


I am wanting to use the phonegap audio api in GWT using JSNI.I cannot figure out how to code the methods in JSNI.

Wondering if anyone know of any tutorials.They javascript methods are really pretty simple.

http://docs.phonegap.com/phonegap_media_media.md.html


Solution

  • Basically it sounds like it would be something like this:

    public final class Media extends JavaScriptObject {
      protected Media() {}
    
      public static native final Media newInstance(String src, Command command) /*-{
        var callback = function() { command.execute(); };
        return new Media(src, callback);
      }-*/;
    
      public native final void getCurrentPosition(AsyncCallback<String> command) /*-{
        var callback = function(position) { command.onSuccess('' + position); };
        this.getCurrentPosition(callback);
      }-*/;
      public native final void play() /*-{
        this.play();
      }-*/;
      //... more methods here
    }
    

    Usage:

    Media m = Media.newInstance("http://www.example.com/src.mp3", new Command() {
      @Override
      public void execute() {
        // Code executed after Media is created.
      }
    });
    m.getCurrentPosition(new AsyncCallback<String>() {
      @Override
      public void onSuccess(String position) {
        Window.alert(position);
      }
    });
    m.play();
    

    That's a rough sketch, if you know more about what the type being passed to the callback is you can do nicer things like have it be an int or another JS Overlay Type.

    The API is kind of weird because everything is apparently asynchronous, but that's life.

    Once you've gotten the hang of writing GWT JSNI bindings it's pretty straightforward.

    If you end up getting further down this road, it would be awesome if you open-sourced your GWT wrapper library so other GWT developers could write some iPhone/Android apps.