logo
20.1
search
No matching documents found.
list
search
No matching documents found.
logo
Please be aware that there is newer version of documentation available for Webswing. Documentation 24.2

Audio Support

Webswing offers simple audio support for playing audio clips. Playing audio through Webswing works only if you use standard Java API. If your application uses a 3rd party library to play audio, this will not work with Webswing.

Here is a short example of playing an audio clip with Webswing.

First prepare a clip.

private Clip prepareClip(String audioFile) {
    try (AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(getClass().getResourceAsStream(audioFile)))) {
        Clip clip = AudioSystem.getClip();
        clip.open(ais);
        return clip;
    } catch (IOException | UnsupportedAudioFileException | LineUnavailableException e1) {
        e1.printStackTrace();
    }
    return null;
}

Now you can play the clip.

Clip alarmClip = prepareClip("resources/audio/alarm.ogg");
alarmClip.start();

Or loop the clip multiple times.

Clip carClip = prepareClip("resources/audio/car.mp3");
carClip.loop(3);

Please check out the SwingSet3 demo application from examples to see a full audio support example.

Limited support

When running your application on desktop, the audio clip is played using the desktop operating system. When you run your application through Webswing, this is not possible since the audio will be played on the server operating system. This is why Webswing overrides the standard audio API and does not actually play the audio clip on the server operating system. Instead, it sends the audio file over the network to user's browser and uses the browser API to play the audio clip.

The fact that the audio clip is played on client side limits the use of standard audio api to following methods:

  • Clip.open
  • Clip.isOpen
  • Clip.start
  • Clip.stop
  • Clip.loop
  • Clip.setMicrosecondPosition

Note that it is not possible to check whether the audio format is supported, use controls, check size/length, use listeners or use advanced positioning and looping.

Since the audio clip is sent over the network to be played by the browser, users may experience a small delay depending on the audio clip size and user's network connection.

Audio format support

The formats that are supported depend on the actual browser where you are running your Webswing application.

Chrome - MP3, WAV, OGG, FLAC

Firefox - MP3, WAV, OGG, FLAC

EDGE (79+) - MP3, WAV, OGG, FLAC

IE11 - MP3