logo
24.2
search
No matching documents found.
list
search
No matching documents found.
logo

Audio

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 our Webswing 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
  • Clip.addLineListener
  • Clip.removeLineListener

Note that it is not possible to check whether the audio format is supported, use controls, check size/length 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.

You can listen to clip events using addLineListener. When the playback stops in browser the listener is notified with STOP event. Note that it is not guaranteed that the event will be fired, for example due to network problems or when the browser connection is forcibly closed.

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

Toolkit.beep customization

The beep sound of the Toolkit.beep method is provided in the client source code. To change it you can customize the audio service.

  1. Create your index.html as described in the Customization
  2. Copy the desired beep sound to your webroot (e.g. beep.mp3)
  3. Adjust the audio service beep function like this:
...
customization: function(injector) {
    const beepAudio = new Audio("./beep.mp3");                
    injector.services.audio.beep = () => beepAudio.play();
},
...