What's new? | Help | Directory | Sign in
Google
gwt-voices
Sound Library for Google-Web-Toolkit (GWT)
  
  
  
  
    
Search
for
Updated Jun 22, 2008 by fredsa
Labels: Featured
GettingStarted  
Getting started with gwt-voices in your own application.

Here's a quick tutorial to using the gwt-voices module in your own application.

While the Eclipse IDE is not strictly necessary, having a good IDE such as Eclispe will make your life a lot easier. Certainly feel free to use your IDE of choice.

GWT Compatibility

GWT Version gwt-voices Version
GWT 1.5.0 (1.5 RC1) gwt-voices-1.5.4.jar
GWT 1.4.62 (1.4 update 2) gwt-voices-1.0.0.jar
GWT 1.4.61 (1.4 update) gwt-voices-1.0.0.jar
GWT 1.4.60 (1.4) gwt-voices-1.0.0.jar

If you can, please switch to the latest GWT version and gwt-voices version.

Adding the gwt-voices Module to your Eclipse Project

  1. Download the latest gwt-voices-<version>.jar and place it in a convenient location.
  2. Create a GWT Eclipse project as instructed here: http://code.google.com/webtoolkit/gettingstarted.html#NewEclipse
  3. mkdir MyProject
    cd MyProject
    projectCreator -eclipse MyProject
    applicationCreator -eclipse MyProject com.mycompany.client.MyApplication
  4. Import your new project via 'File > Import > Existing Project into Workspace'. Specify the MyProject directory you created in a previous step.
  5. To allow the GWT command line scripts to continue to work, modify the -cp parameter value in both MyApplication-shell.cmd and MyApplication-compile.cmd to include the path to gwt-voices-<version>.jar. You can add it just before the path to gwt-user.jar. Don't forget the path separator ';' or ':', depending on your platform.
  6. Right-click on the project node in the Package Explorer and select 'Build Path > Add External Archives...'. Select gwt-voices-<version>.jar.
  7. Make sure GWT can find the gwt-voices module at runtime. Modify MyProject/src/com/mycompany/MyApplication.gwt.xml to include these lines:
  8. 	<!-- Inherit sound support                           -->
    	<inherits name='com.allen_sauer.gwt.voices.gwt-voices'/>

A Quick Example

package com.mycompany.client;

import com.google.gwt.core.client.EntryPoint;

import com.allen_sauer.gwt.voices.client.Sound;
import com.allen_sauer.gwt.voices.client.SoundController;

public class SimpleApplication implements EntryPoint {
  public void onModuleLoad() {
    SoundController soundController = new SoundController();
    Sound sound = soundController.createSound(Sound.MIME_TYPE_AUDIO_MPEG,
        "url/to/your/sound/file.mp3");

    sound.play();
  }
}

A More Elaborate Example

package com.mycompany.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

import com.allen_sauer.gwt.voices.client.Sound;
import com.allen_sauer.gwt.voices.client.SoundController;
import com.allen_sauer.gwt.voices.client.handler.PlaybackCompleteEvent;
import com.allen_sauer.gwt.voices.client.handler.SoundHandler;
import com.allen_sauer.gwt.voices.client.handler.SoundLoadStateChangeEvent;

public class MyApplication implements EntryPoint {
  public void onModuleLoad() {
    // set uncaught exception handler
    GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
      public void onUncaughtException(Throwable e) {
        Window.alert("Uncaught exception:\n" + e);
      }
    });

    // use deferred command to catch initialization exceptions
    DeferredCommand.addCommand(new Command() {
      public void execute() {
        onModuleLoad2();
      }
    });
  }

  public void onModuleLoad2() {
    // create a (disabled) play button
    final Button playButton = new Button("click to play");

    // create a place holder for the load state
    final HTML loadStateHTML = new HTML();

    // create sound controller
    SoundController soundController = new SoundController();

    // create a sound
    final Sound sound = soundController.createSound(Sound.MIME_TYPE_AUDIO_MPEG,
        "url/to/your/sound/file.mp3");

    // add a sound handler so we know when the sound has loaded
    sound.addEventHandler(new SoundHandler() {
      public void onPlaybackComplete(PlaybackCompleteEvent event) {
      }

      public void onSoundLoadStateChange(SoundLoadStateChangeEvent event) {
        loadStateHTML.setHTML("Load state: " + event.getLoadStateAsString());
      }
    });

    // when we click, play the sound
    playButton.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        sound.play();
      }
    });

    // add the button
    RootPanel.get().add(playButton);

    // add the load state status
    RootPanel.get().add(loadStateHTML);
  }
}

Tips

  1. Make sure you use an UncaughtExceptionHandler so that unexpected exceptions are not lost in web mode

Working Examples

There is a working examples along with the demo source code for you to look at.

There's also the library source code if you want to see what makes it tick.


Comment by fredsa, Jun 22, 2008

Thanks! I made the corrections


Sign in to add a comment