Note:
This only describes how to build and compile using Visual C++ Express 2010.
If you are using VS or a variant of VS then you should be fine.
irrKlang currently supports these audio formats:
.wav
.mp3
.ogg
.flac
.mod
.it
.s3d
.xm
The first thing needed is the actual irrKlang library which can be downloaded free (for non-commercial use) from the irrKlang website:
http://www.ambiera.c.../downloads.html
Once you have downloaded the library, unzip it to a place of your choosing.
Next open up Visual Studio and create a new empty Win32 Console Application. If you are unsure how to do that then look here:
Getting Started In Microsoft Visual Studio 2008
I named the project 'irrKlang_test'. The name doesn't matter.
Go to File-Save All and save your project. (Currently the project is temporary and no folders have been created)
Next, open up irrKlang folder that you extracted from before.
Copy the include folder from the library to your current project.
If you are unsure which folder to place include in then look at my folderpath.

Next go into the bin/win32-visualStudio folder and grab
ikpMP3.dll
irrKlang.dll

Move those files to your project as well.
Finally you need to copy the irrKlang.lib library from the irrKlang-1.3.0\lib\Win32-visualStudio folder to your project.
Go back to Visual Studio and add a .cpp file. I called mine "main.cpp" but it doesn't matter.
At this point your project folder should look something like this but without the .mp3 file.

Now open up the newly created cpp file and start coding.
===================================================
================== The Code =========================
===================================================
First we need the proper headers
#include <iostream> #include <string> #include <windows.h> #include "include/irrKlang.h"
The string and windows headers are used for the audio file name and the Sleep() function.
In order to use anything in the irrKlang library you need to use its namespace, irrklang.
To get rid of a lot of typing just use the namespace. (NOTE: LOWERCASE 'K')
using namespace std; using namespace irrklang;
After all the headers and namespaces have been taken care of we need to reference irrKlang.lib so we can actually use the library.
The easy way to do this is to include this line of code:
#pragma comment(lib, "irrKlang.lib")
Now define the main function and create the Sound Engine
int main() { // Creates the Sound Engine with default parameters ISoundEngine* se = createIrrKlangDevice(); return 0; }
What if the sound engine wasn't created properly?
Well, use an if statement to check.
if(!se) { cout << "Error: Sound Engine could not be created" << endl; return 0; }
Your code should now look something similar to this:
#include <iostream> #include <string> #include <windows.h> #include "include/irrKlang.h" using namespace std; using namespace irrklang; #pragma comment(lib, "irrKlang.lib") int main() { // Create the sound engine ISoundEngine* se = createIrrKlangDevice(); // check for errors with creation if(!se) { cout << "Error: Could not create Sound Engine" << endl; return 0; } return 0; }
If all you wanted to do was play a file then you would call
// Note the '->' syntax, se is a pointer se->play2D("somefile.wav"); Sleep(1000);
That code would play somefile.wav for 1 second and then the program would exit.
However, the method isn't very good. Who want's to hard code sleep times in for everything?
Nobody does so I've created an easy way of playing the entire file without having to "sleep" for the exact number of milliseconds.
This is where the <string> header comes in.
Create a new string variable and initialize it to a media file name.
(You could get input from the user here but I didn't)
Now, play the sound. Here is where another what-if comes into play. What if the file name is invalid? Well we can check for that too.
// play2D returns 0 if it is successfully playing if(se->play2D(soundFile.c_str()) != 0) { cout << "Error: Could not play file" << endl; return 0; }
Well now the file is playing but the program will exit in another millisecond.
So lets check and see if the file is still playing
while(se->isCurrentlyPlaying(soundFile.c_str())) Sleep(100);
isCurrentlyPlaying() returns true if the file passed in is currently playing and false otherwise.
That's it your done. Who would have thought all it takes to play music is a short 39 lines of code?
If you want to play something other than motor_bikes.mp3 then substitute the file name in or get input from the enduser.
If you have any questions just ask.
The complete code:
#include <iostream> #include <string> #include <windows.h> #include "include/irrKlang.h" using namespace std; using namespace irrklang; #pragma comment(lib, "irrKlang.lib") int main() { // Create the sound engine ISoundEngine* se = createIrrKlangDevice(); // check for errors with creation if(!se) { cout << "Error: Could not create Sound Engine" << endl; return 0; } string soundFile = "motor_bikes.mp3"; // Play some sound if(se->play2D(soundFile.c_str()) != 0) { cout << "Error: Could not play file" << endl; return 0; } // Keep playing until song/sound has ended while(se->isCurrentlyPlaying(soundFile.c_str())) Sleep(100); se->drop(); return 0; }
To download the complete Visual C++ solution click here

Number of downloads: 936