A while ago I presented a way to write an mp3 player in 16 lines of code. Let’s try to do the same thing with a different sound library, FMOD. FMOD has an advantage over other sound libraries, because it has the same API for different platforms: Win32, Windows 8, Windows phone 8, Macintosh, iOS, Linux, Android, Blackberry and several gaming platforms, like PS3/4/PSP/Vita, Xbox 360/One, Wii etc.
FMOD comes as a native DLL library – fmodex.dll – which is required for the application to run properly. For convenience, we may add it to the project and set its Copy to Output Directory property to Copy always – Visual Studio will take care for copying that DLL for us.
fmodes.dll is native, but fortunately FMOD team provides an almost-complete set of wrappers for C#, so we won’t have to P/Invoke our way to play a sound.
Let’s start with adding a few fields to our class.
[csharp] private FMOD.System system;
private FMOD.Sound sound;
private FMOD.Channel channel;[/csharp]
System is the core of FMOD – it creates sound objects, allows starting playback etc.
Sound is a class representing the actual sound, being played. Finally,
Channel is a class, which represents the actual playback process and allows modifying its aspects – for instance, the volume.
System object can be created with aid of
FMOD.Factory. We may create and initialize it in form’s constructor, as there’s usually no point in keeping several instances of
system.init(1, FMOD.INITFLAGS.NORMAL, IntPtr.Zero);
Now we can implement the Open & Play button. Fortunately,
System.createSound method accepts filename as its first parameter, so the process will be as easy as in case of NAudio.
[csharp]private void bPlay_Click(object sender, EventArgs e)
OpenFileDialog dialog = new OpenFileDialog()
Filter = "Sound files (*.mp3;*.wav;*.wma)|*.mp3;*.wav;*.wma"
if (dialog.ShowDialog() == DialogResult.OK)
system.createSound(dialog.FileName, FMOD.MODE.DEFAULT, ref sound);
system.playSound(FMOD.CHANNELINDEX.FREE, sound, false, ref channel);
Stopping playback is also quite an easy task – it’s only a matter of asking a
Channel object to stop.
[csharp]private void bStop_Click(object sender, EventArgs e)
if (channel != null)
And that’s all we need to play a sound. Keep in mind, that this example program does not provide any error checking – all FMOD methods return
FMOD.Result, which informs about outcome of the operation.