In this section we'll examine a method to play various media files like MP3 and the fantastic OGG format using the FMOD library.
FMOD is a cross-platform music and audio library. It's not free for commercial development, however, and the price varies depending on a developer's product and situation. The FMOD SDK can be downloaded for free from http://www.fmod.org/.
Figure 20.1
Some of FMOD's features and properties are listed below. Supported Platforms
Linux
Linux 64-bit (AMD64)
Macintosh OS8/9/X and OSX for x86
Microsoft Windows series
Microsoft Xbox
Microsoft Xbox 360
Nintendo GameCube
Nintendo Wii
Sony PlayStation 2
Sony PlayStation 3
Sony PlayStation Portable
Supported Audio File Formats
AIFF-Audio Interchange File Format
ASF-Advanced Systems Format
ASX-Advanced Stream Redirector playlist format
DLS-DownLoadable Sound format for MIDI playback
FLAC-Free Lossless Audio Codec
FSB-FMOD Sample Bank format generated by FSBank and FMOD designer tool
M3U (also known as MP3 URL)-Moving Picture Experts Group Audio Layer 3 Uniform Resource Locator playlist format
MID-MIDI (Musical Instrument Digital Interface) using operating system or custom DLS patches
MOD-Module file format
MP2-MPEG-1/2 Audio Layer 2
MP3-MPEG-1/2 Audio Layer 3
OGG-Ogg Vorbis format
PLS-Playlist format
RAW-Raw file format
S3M-ScreamTracker 3 sequenced MOD format
VAG-PS2/PSP format
WAV-Microsoft Waveform audio format
WAX-Windows Media Audio Redirector playlist format
WMA-Windows Media Audio format
XM-Extended Module format
XMA-Xbox compressed WMA format
Once downloaded, FMOD is easy to install using the wizard, which installs FMOD to the computer. All the libraries and documentation will be stored in the FMOD folder. To use FMOD, the fmod.hpp file must be included in projects using the #include preprocessor directive; for example, #include<fmod.hpp>. For Visual Studio, the library fmodex_vc.lib should be linked, and for Code::Blocks and Dev C++, the libfmodex.a library should be linked.
The process of playing a sound file in FMOD is quite simple. The following sample program demonstrates how a sound file can be played.
FMOD_RESULT result; FMOD::Sound *sound; FMOD::Channel *channel; FMOD::System *system; result = FMOD::System_Create(&system); system->init(200, FMOD_INIT_NORMAL, 0); system->createSound("sample.mp3", FMOD_DEFAULT, 0, &sound); system->playSound(FMOD_CHANNEL_FREE, sound, false, 0);
That's all there is to it. The above code creates two important objects: FMOD::Sound and FMOD::System. FMOD::System represents the actual sound playing hardware attached to the computer, and it acts like the media player controls on a stereo, allowing programmers to start, stop, and pause playback of music. The FMOD::Sound class encapsulates the actual music file, in this case sample.mp3. Naturally, this could be any other sound file in a valid format. Finally, the playSound method of FMOD::System requires a sound to play, a channel to play in, and a flag to determine whether the sound should be repeated.