ambiera home ambiera home

irrKlang Tutorial: Hello World (C#)

This example will show how to play sounds using irrKlang. It will play a looped background music and a sound every time the user presses a key.

Let's start

At the beginning, we simply create a class and add the namespace IrrKlang in which all sound engine classes are located.

using System;
using IrrKlang;

namespace CSharp._01.HelloWorld
{
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {

Now lets start with irrKlang 3D sound engine example 01, demonstrating simple 2D sound. Start up the sound engine by creating an instance of the class ISoundEngine. You can specify several options as parameters when invoking that constructor, but for this example, the default parameters are enough.

    // start the sound engine with default parameters
			
    ISoundEngine engine = new ISoundEngine();

To play a sound, we only to call play2D(). The second parameter tells the engine to play it looped.

   // play some sound stream, looped
   
   engine.Play2D("../../media/getout.ogg", true);

In a loop, wait until user presses 'q' to exit or another key to play another sound.

    do
    {
      Console.Out.WriteLine("Press any key to play some sound, press 'q' to quit.");

      // play a single sound
      engine.Play2D("../../media/bell.wav");
    }
    while(_getch() != 'q');

Basically, that's it. The following code just closes the classes and namespace and adds a method _getch() to read from the console, but this has nothing to do with irrKlang.

    }
			
    // some simple function for reading keys from the console
    [System.Runtime.InteropServices.DllImport("msvcrt")]
    static extern int _getch();
  }
}

Have fun playing your own sounds now.
Download tutorial source and binary (included in the SDK)