Version: 3.2.5
wxSound Class Reference

#include <wx/sound.h>

+ Inheritance diagram for wxSound:

Detailed Description

This class represents a short sound (loaded from Windows WAV file), that can be stored in memory and played.

Currently this class is implemented on Windows and Unix and can use either Open Sound System (OSS) or Simple DirectMedia Layer (SDL) under the latter. Notice that OSS is not provided any more by most, and maybe even all, Linux systems in 2017 and osspd (OSS Proxy Daemon) package typically needs to be installed to make it work.

Library:  wxCore
Category:  Multimedia
bool Play (unsigned flags=wxSOUND_ASYNC) const
 Plays the sound file. More...
 
static bool Play (const wxString &filename, unsigned flags=wxSOUND_ASYNC)
 Plays the sound file. More...
 

Public Member Functions

 wxSound ()
 Default ctor. More...
 
 wxSound (const wxString &fileName, bool isResource=false)
 Constructs a wave object from a file or, under Windows, from a Windows resource. More...
 
 wxSound (size_t size, const void *data)
 Constructs a wave object from in-memory data. More...
 
virtual ~wxSound ()
 Destroys the wxSound object. More...
 
bool Create (const wxString &fileName, bool isResource=false)
 Constructs a wave object from a file or resource. More...
 
bool Create (size_t size, const void *data)
 Constructs a wave object from in-memory data. More...
 
bool IsOk () const
 Returns true if the object contains a successfully loaded file or resource, false otherwise. More...
 
- Public Member Functions inherited from wxObject
 wxObject ()
 Default ctor; initializes to NULL the internal reference data. More...
 
 wxObject (const wxObject &other)
 Copy ctor. More...
 
virtual ~wxObject ()
 Destructor. More...
 
virtual wxClassInfoGetClassInfo () const
 This virtual function is redefined for every class that requires run-time type information, when using the wxDECLARE_CLASS macro (or similar). More...
 
wxObjectRefDataGetRefData () const
 Returns the wxObject::m_refData pointer, i.e. the data referenced by this object. More...
 
bool IsKindOf (const wxClassInfo *info) const
 Determines whether this class is a subclass of (or the same class as) the given class. More...
 
bool IsSameAs (const wxObject &obj) const
 Returns true if this object has the same data pointer as obj. More...
 
void Ref (const wxObject &clone)
 Makes this object refer to the data in clone. More...
 
void SetRefData (wxObjectRefData *data)
 Sets the wxObject::m_refData pointer. More...
 
void UnRef ()
 Decrements the reference count in the associated data, and if it is zero, deletes the data. More...
 
void UnShare ()
 This is the same of AllocExclusive() but this method is public. More...
 
void operator delete (void *buf)
 The delete operator is defined for debugging versions of the library only, when the identifier __WXDEBUG__ is defined. More...
 
void * operator new (size_t size, const wxString &filename=NULL, int lineNum=0)
 The new operator is defined for debugging versions of the library only, when the identifier __WXDEBUG__ is defined. More...
 

Static Public Member Functions

static bool IsPlaying ()
 Returns true if a sound is played at the moment. More...
 
static void Stop ()
 If a sound is played, this function stops it. More...
 

Additional Inherited Members

- Protected Member Functions inherited from wxObject
void AllocExclusive ()
 Ensure that this object's data is not shared with any other object. More...
 
virtual wxObjectRefDataCreateRefData () const
 Creates a new instance of the wxObjectRefData-derived class specific to this object and returns it. More...
 
virtual wxObjectRefDataCloneRefData (const wxObjectRefData *data) const
 Creates a new instance of the wxObjectRefData-derived class specific to this object and initializes it copying data. More...
 
- Protected Attributes inherited from wxObject
wxObjectRefDatam_refData
 Pointer to an object which is the object's reference-counted data. More...
 

Constructor & Destructor Documentation

◆ wxSound() [1/3]

wxSound::wxSound ( )

Default ctor.

◆ wxSound() [2/3]

wxSound::wxSound ( const wxString fileName,
bool  isResource = false 
)

Constructs a wave object from a file or, under Windows, from a Windows resource.

Call IsOk() to determine whether this succeeded.

Parameters
fileNameThe filename or Windows resource.
isResourcetrue if fileName is a resource, false if it is a filename.

◆ wxSound() [3/3]

wxSound::wxSound ( size_t  size,
const void *  data 
)

Constructs a wave object from in-memory data.

Parameters
sizeSize of the buffer pointer to by data.
dataThe buffer containing the sound data in WAV format.

◆ ~wxSound()

virtual wxSound::~wxSound ( )
virtual

Destroys the wxSound object.

Member Function Documentation

◆ Create() [1/2]

bool wxSound::Create ( const wxString fileName,
bool  isResource = false 
)

Constructs a wave object from a file or resource.

Parameters
fileNameThe filename or Windows resource.
isResourcetrue if fileName is a resource, false if it is a filename.
Returns
true if the call was successful, false otherwise.

◆ Create() [2/2]

bool wxSound::Create ( size_t  size,
const void *  data 
)

Constructs a wave object from in-memory data.

Parameters
sizeSize of the buffer pointer to by data.
dataThe buffer containing the sound data in WAV format.

◆ IsOk()

bool wxSound::IsOk ( ) const

Returns true if the object contains a successfully loaded file or resource, false otherwise.

◆ IsPlaying()

static bool wxSound::IsPlaying ( )
static

Returns true if a sound is played at the moment.

This method is currently not available under Windows and may not be always implemented in Unix ports depending on the compilation options used (in this case it just always returns false).

Availability:  only available for the wxGTK, wxOSX/Cocoa ports.

◆ Play() [1/2]

static bool wxSound::Play ( const wxString filename,
unsigned  flags = wxSOUND_ASYNC 
)
static

Plays the sound file.

If another sound is playing, it will be interrupted.

Returns true on success, false otherwise. Note that in general it is possible to delete the object which is being asynchronously played any time after calling this function and the sound would continue playing, however this currently doesn't work under Windows for sound objects loaded from memory data.

The possible values for flags are:

  • wxSOUND_SYNC: Play will block and wait until the sound is replayed.
  • wxSOUND_ASYNC: Sound is played asynchronously, Play returns immediately.
  • wxSOUND_ASYNC|wxSOUND_LOOP: Sound is played asynchronously and loops until another sound is played, Stop() is called or the program terminates.

The static form is shorthand for this code:

wxSound(filename).Play(flags);
wxSound()
Default ctor.

◆ Play() [2/2]

bool wxSound::Play ( unsigned  flags = wxSOUND_ASYNC) const

Plays the sound file.

If another sound is playing, it will be interrupted.

Returns true on success, false otherwise. Note that in general it is possible to delete the object which is being asynchronously played any time after calling this function and the sound would continue playing, however this currently doesn't work under Windows for sound objects loaded from memory data.

The possible values for flags are:

  • wxSOUND_SYNC: Play will block and wait until the sound is replayed.
  • wxSOUND_ASYNC: Sound is played asynchronously, Play returns immediately.
  • wxSOUND_ASYNC|wxSOUND_LOOP: Sound is played asynchronously and loops until another sound is played, Stop() is called or the program terminates.

The static form is shorthand for this code:

wxSound(filename).Play(flags);

◆ Stop()

static void wxSound::Stop ( )
static

If a sound is played, this function stops it.