How to play mp3, ogg, wav, mod and other sound files in Windows.Forms and C#

All the tutorials at the irrKlang page are using a console as example application type, because I thought it would be more simple to understand, but a lot of people asked me how to use irrKlang together with Windows.Forms. So here is a small example code, showing how to play back any sound file (mp3, ogg, wav, whatever) using irrKlang, in a small Windows.Forms application:

You only need to download irrKlang and add the irrKlang.NET.DLL file as reference to your project.

// Minimal Windows.Forms program in CSharp
// to play back an .ogg, mp3, .mod, .wav etc file:

using System;
using System.Windows.Forms;

namespace PlayerApplication
{
public class PlayerWindow : Form
{
irrKlang.ISoundEngine soundEngine = new irrKlang.ISoundEngine();
irrKlang.ISound currentlyPlayingSound;

[STAThread]
static void Main()
{
Application.Run(new PlayerWindow());
}

public PlayerWindow()
{
// setup window
Button playButton = new Button();
Button stopButton = new Button();
playButton.Location = new System.Drawing.Point(48, 78);
playButton.Text = "Play";
playButton.Click += new System.EventHandler(PlayButton_Click);
stopButton.Location = new System.Drawing.Point(173, 78);
stopButton.Text = "Stop";
stopButton.Click += new System.EventHandler(StopButton_Click);
Controls.Add(stopButton);
Controls.Add(playButton);
}

private void PlayButton_Click(object sender, EventArgs e)
{
// replace the filename with the file you want to play.
// copy the ikpmp3.dll to your bin\Debug (or working) path if you want
// to play back mp3 files


currentlyPlayingSound =
soundEngine.Play2D(@"C:\music\someFile.ogg");
}

private void StopButton_Click(object sender, EventArgs e)
{
if (currentlyPlayingSound != null)
{
currentlyPlayingSound.Stop();
currentlyPlayingSound = null;
}
}
}
}

I really don't understand what the big deal is, it should not be that difficult. :)

two comments, already:

I never really used IrrKlang before, I think my question could be answered by just reading the docs, but anyway: how are the sound objects disposed? :-)

Shouldn’t you be calling something like
currentlyPlayingSound.Dispose()
when you play a new sound or does the engine take care of everything? How do you manage resources in this case?
Lorenz Cuno Klopfenstein (link) - 29 07 08 - 13:18

Basically, irrKlang ist written in C++ and the sound engine manages the resources nearly by itself, so you don’t have to think much about this. The data in the ISound interface will be garbage collected automaticly by the .net gc, and when this happens, it usually only frees a few bytes of the managed code. But of course, you still could call dispose yourself earlier as well.
niko - 29 07 08 - 18:29


Name:  
Remember personal info?
yes
no
Email (optional):
URL (optional):
Enter "layered" (antispam):
Comment:Emoticons / Textile

  ( Register your username / Log in )

Notify: Yes, send me email when someone replies.  

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Note: If you type in your email adress above, it will be visible to other visitors, although it will be hidden for bots using javaScript.