Media — video, audio, PDF and other resources

Sound

WSound is a value class to play a sound effect. It provides a way to play an MP3 sound asynchonously (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
#include <Wt/WContainerWidget.h>
#include <Wt/WPushButton.h>
#include <Wt/WSound.h>
#include <Wt/WText.h>

auto container = Wt::cpp14::make_unique<Wt::WContainerWidget>();

auto sound = container->addChild(
	Wt::cpp14::make_unique<Wt::WSound>("sounds/beep.mp3"));
sound->setLoops(3);

auto playButton =
    container->addWidget(Wt::cpp14::make_unique<Wt::WPushButton>("Beep!"));
playButton->setMargin(5);

auto stopButton =
    container->addWidget(Wt::cpp14::make_unique<Wt::WPushButton>("Stop it!"));
stopButton->setMargin(5);

Wt::WText *out =
    container->addWidget(Wt::cpp14::make_unique<Wt::WText>());

playButton->clicked().connect([=] {
    sound->play();
    out->setText("<p>Beeping started!</p>");
});

stopButton->clicked().connect([=] {
    sound->stop();
    out->setText("<p>Beeping stopped!</p>");
});