Example usage for com.google.gwt.media.client Audio getAudioElement

List of usage examples for com.google.gwt.media.client Audio getAudioElement

Introduction

In this page you can find the example usage for com.google.gwt.media.client Audio getAudioElement.

Prototype

public AudioElement getAudioElement() 

Source Link

Document

Returns the attached AudioElement.

Usage

From source file:es.deusto.weblab.client.ui.audio.AudioManager.java

License:Open Source License

/**
 * Helper method which will play a sound if certain conditions are met:
 * Sound is enabled./*from  w w  w .  j a va  2 s. c o  m*/
 * Sound is supported.
 * File type is supported.
 * If they are not, calling this method will have no effect.
 * @param file File to play. The path should be relative to the module base URL. E.g. "/audio/foo.wav"
 * 
 * @return AudioElement being played, or null. May be used to modify the default behaviour, such
 * as enabling loop mode.
 */
public AudioElement play(String file) {
    if (this.getSoundEnabled()) {
        final Audio audio = Audio.createIfSupported();
        if (audio != null) {
            final AudioElement elem = audio.getAudioElement();
            elem.setSrc(GWT.getModuleBaseURL() + file);
            elem.play();
            return elem;
        }
    }
    return null;
}

From source file:es.deusto.weblab.client.ui.audio.AudioManager.java

License:Open Source License

/**
 * Helper method which will play a sound if certain conditions are met:
 * Sound is enabled.//from  www  .ja  v  a 2 s  .co m
 * Sound is supported.
 * If they are not, calling this method will have no effect.
 * 
 * The file type is inferred by the web browser. If it supports OGG, it will use the ".ogg" extension. If it doesn't, but it
 * supports MP3, it will use ".mp3". It will also try WAV, and otherwise it will not work. Therefore, the audio files must be 
 * duplicated in these three formats to work. 
 * 
 * @param file File to play. The path should be relative to the module base URL. E.g. "/audio/foo" will select foo.ogg, foo.mp3 or foo.wav
 * 
 * @return AudioElement being played, or null. May be used to modify the default behaviour, such
 * as enabling loop mode.
 */
public AudioElement playBest(String file) {
    if (this.getSoundEnabled()) {
        final Audio audio = Audio.createIfSupported();
        if (audio != null) {
            final AudioElement elem = audio.getAudioElement();

            // First try probably
            if (elem.canPlayType(OGG_TYPE).equals(MediaElement.CAN_PLAY_PROBABLY))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".ogg");
            else if (elem.canPlayType(MP3_TYPE).equals(MediaElement.CAN_PLAY_PROBABLY))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".mp3");
            else if (elem.canPlayType(WAV_TYPE).equals(MediaElement.CAN_PLAY_PROBABLY))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".wav");
            // Then maybe
            else if (elem.canPlayType(OGG_TYPE).equals(MediaElement.CAN_PLAY_MAYBE))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".ogg");
            else if (elem.canPlayType(MP3_TYPE).equals(MediaElement.CAN_PLAY_MAYBE))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".mp3");
            else if (elem.canPlayType(WAV_TYPE).equals(MediaElement.CAN_PLAY_MAYBE))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".wav");
            // Then fail
            else
                return null;

            elem.play();
            return elem;
        }
    }
    return null;
}

From source file:es.deusto.weblab.client.ui.widgets.WlTimer.java

License:Open Source License

protected void decrement() {
    if (this.time > 0) {
        --this.time;
        this.updateTimeString();
    }//w  w  w. java  2s  . c  om

    if (AudioManager.getInstance().getSoundEnabled()) {
        Audio timeRunningOutAudio = Audio.createIfSupported();
        if (this.time == this.timeRunningOutAudioLimit && AudioManager.getInstance() != null) {
            System.out.println(this.getParent());
            final AudioElement effect = timeRunningOutAudio.getAudioElement();
            effect.setSrc(GWT.getModuleBaseURL() + "snd/clock.wav");
            effect.setLoop(true);
            effect.play();
        } else if (this.time == 0) {
            final AudioElement effect = timeRunningOutAudio.getAudioElement();
            effect.setLoop(false);
            effect.pause();

            // TODO: Make sure that this isn't a memory leak. There does not seem to be any method that provides a 
            // "destroy" rather than "pause". Make also sure that there is no case under which a timer can not reach zero.
            // Otherwise, the sound might play forever, which is significantly annoying.
        }
    }

}