Wt: Wt::WSound Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | List of all members
Wt::WSound Class Reference

A value class to play a sound effect. More...

`#include <Wt/WSound.h>`

Inheritance diagram for Wt::WSound:
[legend]

## Public Member Functions
| WSound ()
| Constructs a sound object.
| WSound (const std::string &url)
| Constructs a sound object for an MP3 media source.
| WSound (MediaEncoding encoding, const WLink &link)
| Constructs a sound object.
| ~WSound ()
| Destructor.
void | addSource (MediaEncoding encoding, const WLink &link)
| Adds a media source.
WLink | getSource (MediaEncoding encoding) const
| Returns the media source.
void | setLoops (int number)
| Sets the amount of times the sound has to be repeated.
int | loops () const
| Returns the configured number of repeats.
void | play ()
| Start asynchronous playback of the sound.
void | stop ()
| Stops playback of the sound.
Public Member Functions inherited from Wt::WObject
void | addChild (std::unique_ptr< WObject > child)
| Add a child WObject whose lifetime is determined by this WObject.
template<typename Child >
Child * | addChild (std::unique_ptr< Child > child)
| Add a child WObject, returning a raw pointer.
std::unique_ptr< WObject > | removeChild (WObject *child)
| Remove a child WObject, so its lifetime is no longer determined by this WObject.
template<typename Child >
std::unique_ptr< Child > | removeChild (Child *child)
| Remove a child WObject, so its lifetime is no longer determined by this WObject.
virtual const std::string | id () const
| Returns the (unique) identifier for this object.
virtual void | setObjectName (const std::string &name)
| Sets an object name.
virtual std::string | objectName () const
| Returns the object name.
void | resetLearnedSlots ()
| Resets learned stateless slot implementations.
template<class T >
void | resetLearnedSlot (void(T::*method)())
| Resets a learned stateless slot implementation.
template<class T >
WStatelessSlot * | implementStateless (void(T::*method)())
| Declares a slot to be stateless and learn client-side behaviour on first invocation.
template<class T >
WStatelessSlot * | implementStateless (void(T::*method)(), void(T::*undoMethod)())
| Declares a slot to be stateless and learn client-side behaviour in advance.
void | isNotStateless ()
| Marks the current function as not stateless.
template<class T >
WStatelessSlot * | implementJavaScript (void(T::*method)(), const std::string &jsCode)
| Provides a JavaScript implementation for a method.
Public Member Functions inherited from Wt::Core::observable
| observable () noexcept
| Default constructor.
virtual | ~observable ()
| Destructor.
template<typename... Args, typename C >
auto | bindSafe (void(C::*method)(Args...)) noexcept
| Protects a method call against object destruction.
template<typename... Args, typename C >
auto | bindSafe (void(C::*method)(Args...) const) const noexcept
| Protects a const method call against object destruction.
template<typename Function >
auto | bindSafe (const Function &function) noexcept
| Protects a function against object destruction.

## Additional Inherited Members
Public Types inherited from Wt::WObject
typedef void(WObject::*) | Method()
| Typedef for a WObject method without arguments.
Static Public Member Functions inherited from Wt::WObject
Protected Member Functions inherited from Wt::WObject
virtual WStatelessSlot * | getStateless (Method method)
| On-demand stateless slot implementation.

## Detailed Description

A value class to play a sound effect.

This class 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 for a media center).

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

Usage example:

WSound *s = parent->addChild(std::make_unique<WSound>("djing.mp3"));
s->setLoops(3);
s->play();
playButton->clicked().connect(s, &WSound::play);
stopButton->clicked().connect(s, &WSound::stop);
Wt::WObject::addChild
void addChild(std::unique_ptr< WObject > child)
Add a child WObject whose lifetime is determined by this WObject.
Wt::WSound
A value class to play a sound effect.
Definition WSound.h:38
Wt::WSound::play
void play()
Start asynchronous playback of the sound.
Wt::WSound::setLoops
void setLoops(int number)
Sets the amount of times the sound has to be repeated.
Wt::WSound::stop
void stop()
Stops playback of the sound.

## Constructor & Destructor Documentation

## ◆ WSound() [1/3]

Wt::WSound::WSound | ( |  | ) |

Constructs a sound object.

See also
  addSource()

## ◆ WSound() [2/3]

Wt::WSound::WSound | ( | const std::string & | url | ) |

Constructs a sound object for an MP3 media source.

The `url` will be assumed to be an MP3 file.

See also
  addSource()

## ◆ WSound() [3/3]

Wt::WSound::WSound | ( | MediaEncoding | encoding,
|  | const WLink & | link )

Constructs a sound object.

See also
  addSource()

## ◆ ~WSound()

Wt::WSound::~WSound | ( |  | ) |

Destructor.

Deleting a sound also stops it (if it was playing).

## Member Function Documentation

## ◆ addSource()

void Wt::WSound::addSource | ( | MediaEncoding | encoding,
|  | const WLink & | link )

Adds a media source.

You may add multiple media sources (with different encodings) to allow the file to be played in more browsers without needing Flash plugins.

## ◆ getSource()

WLink Wt::WSound::getSource | ( | MediaEncoding | encoding | ) | const

Returns the media source.

This returns the link set for a specific encoding, or an empty link if no URL was set for that encoding.

## ◆ loops()

int Wt::WSound::loops | ( |  | ) | const

Returns the configured number of repeats.

setLoops()

## ◆ play()

void Wt::WSound::play | ( |  | ) |

Start asynchronous playback of the sound.

This method returns immediately. It will cause the sound to be played for the configured amount of loops().

The behavior of play() when a sound is already playing is undefind: it may be intermixed, sequentially queued, or a current playing sound may be stopped. It is recommended to call stop() before play() if you want to avoid mixing multiple instances of a single WSound object.

## ◆ setLoops()

void Wt::WSound::setLoops | ( | int | number | ) |

Sets the amount of times the sound has to be repeated.

A call to play() will play the sound `number` of times. The default value is 1 (no repeats).

## ◆ stop()

void Wt::WSound::stop | ( |  | ) |

Stops playback of the sound.

This method returns immediately. It causes the current playback (if any) of the sound to be stopped.