Knowledge Base Nr: 00337 soundgwt.java - http://www.swe-kaiser.de

GWT: Soundausgabe im Browser

  

/* aufrufbeispiel *&

private CSound m_sound = null;

private String PlaySoundUntilQuit()
{
m_sound = CSound.get();

if (m_sound != null)
{
m_sound.playIncomingSOS();
}

CDialogBoxSOS sosDlg = new CDialogBoxSOS(sSOS, "OK", new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
Widget sender = (Widget) event.getSource();
CDialogBoxSOS dlg = (CDialogBoxSOS)(sender.getParent().getParent().getParent());
dlg.hide();

if (m_sound != null)
{
m_sound.stop();
}
}
});
sosDlg.center();
sosDlg.show();
}

/* modifiziertes google snippet */

package info.zeitpad.portal.client;
import com.google.gwt.dom.client.AudioElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.media.client.Audio;

import java.util.ArrayList;
import java.util.List;

/**
* A bundle of sound effects that can be used in the application.
*/
public class CSound {

/**
* The source path and type of an audio source file.
*/
private static class AudioSource {
private final String source;
private final AudioType type;

public AudioSource(String source, AudioType type) {
this.source = source;
this.type = type;
}
}

/**
* The supported audio types.
*/
private static enum AudioType {
OGG("audio/ogg"), MP3("audio/mp3"), WAV("audio/wav");

private final String mimeType;

private AudioType(String mimeType) {
this.mimeType = mimeType;
}

public String getMimeType() {
return mimeType;
}
}

private static List<AudioType> typePreference = new ArrayList<AudioType>();

private static CSound instance;
private static boolean isSupported;

/**
* Get the singleton instance.
*
* @return the singleton instance
*/
public static CSound get() {
if (instance == null) {
isSupported = Audio.isSupported();
instance = new CSound();

// Detect which audio types we support.
if (isSupported) {
AudioElement elem = Document.get().createAudioElement();

// Prefer "can play probably" to "can play maybe".
for (AudioType audioType : AudioType.values()) {
if (AudioElement.CAN_PLAY_PROBABLY.equals(elem
.canPlayType(audioType.getMimeType()))) {
typePreference.add(audioType);
}
}

// Use "can play maybe" if its the only thing available.
for (AudioType audioType : AudioType.values()) {
if (AudioElement.CAN_PLAY_MAYBE.equals(elem
.canPlayType(audioType.getMimeType()))) {
typePreference.add(audioType);
}
}
}
}
return instance;
}

/**
* Create and return an audio source.
*/
private static AudioSource audioSource(String source, AudioType type)
{
AudioSource as = new AudioSource(source, type);
return as;
}

private AudioElement sosSound;

/**
* Construct using {@link #get()}.
*/
private CSound() {
}

/**
* Play an error sound, if audio is supported.
*/
public void playIncomingSOS() {
prefetchError();
playAudio(sosSound);
}

public void stop()
{
if (sosSound != null)
sosSound.pause();
}

/**
* Prefetch the error sound.
*/
public void prefetchError() {
if (isSupported && sosSound == null) {
sosSound = createAudioElement(
//audioSource("sound/sos-ring.ogg", AudioType.OGG),
audioSource("sound/sos-ring.mp3", AudioType.MP3),
audioSource("sound/sos-ring.wav", AudioType.WAV));
}
}

/**
* Create an {@link Audio} that will play one of the specified source media
* files. The sources will be tried in the order they are added until a
* supported format is found.
*
* <p>
* This method will attempt to prefetch the audio sources by playing the
* file muted.
* </p>
*
* @param sources
* the source files, of which one will be chosen
* @return a new {@link AudioElement}, or null if not supported
*/
private AudioElement createAudioElement(AudioSource... sources) {
if (!isSupported) {
return null;
}

AudioSource bestSource = null;
for (int i = 0; i < typePreference.size() && bestSource == null; i++) {
AudioType type = typePreference.get(i);
for (AudioSource source : sources) {
if (source.type == type) {
bestSource = source;
break;
}
}
}

// None of the source files are supported.
if (bestSource == null) {
return null;
}

// Create the audio element.
AudioElement audio = Document.get().createAudioElement();
audio.setSrc(bestSource.source);

// Force the browser to fetch the source files.
audio.setVolume(0.0);
audio.play();

return audio;
}

/**
* Play an audio element.
*
* @param audio
* the audio element to play, or null if not supported
*/
private void playAudio(AudioElement audio) {
if (audio == null) {
return;
}

// Pause current progress.
audio.pause();

/*
* Some browsers throw an error when we try to seek back to time 0, so
* reset the source instead. The audio file should be loaded from the
* browser cache.
*/
audio.setSrc(audio.getSrc());

// Unmute because we muted in createAudioElement.
audio.setVolume(1.0);
audio.setLoop(true);
audio.play();
}
}