Since GWT doesn't offer a native library supporting sound it is necessary to use an external gwt library. I ended up using GWT-sound, which in essence is a GWT wrapper of the JavaScript library SoundManager 2 that provides functionalities for playing audio.
To implement the audio player, the following steps were executed:
1. Create the GUI
2. Implement the GUI functionality
3. Implement the sound functionality
4. Initialize the audio player
For step 1 I created two buttons, one for play/resume/pause and one for stopping the sound file entirely:Button play = new Button("play");
Button stop = new Button("stop");
Step 2 was implementing the functionality of the GUI, which in essence should just call the respective sound methods by a click on a button:play.addClickHandler(new ClickHandler() {//onClick call play() or toggle()});
stop.addClickHandler(new ClickHandler() {//onClick call stop()});
To implement the audio functionality the class variable soundmanager was introduced that would handle the sound. SoundManager uses a String to identify a sound file:SoundManager soundmanager = SoundManager.getInstance();
final String soundId = "id";
public void play() {soundmanager.play(soundId);}
public void toggle() {soundmanager.togglePause(soundId);} //SoundManager uses same method for resuming and pausing
public void stop() {soundmanager.stop(soundId);}
In the last step it was necessary to load the sound file into the SoundManager and ensure the audio player wouldn't be played before the files were loaded. Otherwise it would result into an exception. For that purpose the buttons were disabled until the files were finished loading://disable buttons
...
//Assign the sound file to the soundmanager
sm.onLoad(new Callback() {
public void execute() {
soundmanager.createSound(soundId, "pathToTheFile");
}});
//enable the buttons after the sound file is assigned
soundmanager.getDefaultOptions().onLoad(new Callback() {
public void execute() {
//enable buttons
...
}});