Game Development Reference
In-Depth Information
sf::Music and sf::SoundStream
Playing an audio file using the Music class is as simple as:
When we open a music stream from a file, the supported audio formats are the same as they
are for Sound . Also, Music::play() launches the sound in another thread, so we don't
have to worry about blocking the current thread.
A music file can also be streamed from a loaded file in memory
( Music::openFromMemory() ) or from InputStream
( Music::openFromStream() ).
Note how we open from file rather than load from file (as it is with SoundBuffer ). The
music class derives from a class called SoundStream , which implements a common be-
havior to stream audio. It doesn't load all of the data in system memory, but it loads small
chunks of the whole asset and works with them. When it reaches an area of the audio that is
not in the memory, it requests more data from the stream and starts working with that. The
Music class works on top of that interface and just provides methods to open a stream
(with the Music::open*() methods) and feeds these to the SoundStream beneath.
Since we are dealing with a data stream, the data cannot be destroyed while
SoundStream is using it—when we use data which has already been allocated in system
memory, we have to be sure to keep it alive.
As mentioned earlier, the Music class doesn't provide much functionality over
SoundStream , apart from methods which open file streams. All the interesting function-
ality is located in the base class, SoundStream , where methods such as
SoundStream::play() , SoundStream::pause() , and
SoundStream::stop() are located. These three methods work in the exact same way
as their cousins from the Sound class. The SoundStream::setPlayingOffset()
and SoundStream::setLoop() methods are here as well.
Search WWH ::




Custom Search