Sound

WSound is a value class to play a sound effect. It provides a way to play an MP3 sound asynchronously (if the browser supports this). It is intended as a simple way to play event sounds (not quite a media center).

The WSound class uses a WMediaPlayer to play the sound (using HTML <audio> or a flash player).

The following example creates a beep sound that will be repeated 3 times.

Example
source
  void Sound() {
    WContainerWidget container = new WContainerWidget();
    final WSound sound = new WSound("sounds/beep.mp3");
    sound.setLoops(3);
    WPushButton playButton = new WPushButton("Beep!", (WContainerWidget) container);
    playButton.setMargin(new WLength(5));
    WPushButton stopButton = new WPushButton("Stop it!", (WContainerWidget) container);
    stopButton.setMargin(new WLength(5));
    final WText out = new WText((WContainerWidget) container);
    playButton
        .clicked()
        .addListener(
            this,
            () -> {
              sound.play();
              out.setText("<p>Beeping started!</p>");
            });
    stopButton
        .clicked()
        .addListener(
            this,
            () -> {
              sound.stop();
              out.setText("<p>Beeping stopped!</p>");
            });
  }