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

Posted on:July 28 2008

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. :)





Comments:


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 Klop
Quote
2008-07-29 13:18:00


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
Quote
2008-07-29 18:29:00


Add comment:


Posted by:


Enter the missing letter in: "Interna?ional"


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons