c++stringtype-conversiongetlineirrklang

How to play a sound file with irrKlang, to which the path was inputted by the user


I am trying to make a music player in C++ with irrKlang. I want to make it so that you can enter the sound file's path with getline(cin, filename) and the engine plays that sound file for you. The problem is, getline() returns type string whereas irrKlang requires its own type ISoundSource*. I can't use cin either, as any space in the file path will just cut off everything after the character before it.

For example:

#include <iostream>
#include <string>
#include <irrKlang.h>

using namespace std;
using namespace irrklang;

int main()
{
    ISoundEngine* engine = createIrrKlangDevice();
    cout << "Enter path to sound: ";
    string filename;
    getline(cin, filename) //returns string
/*
    ISoundSource* ikfilename;
    getline(cinm ikfilename) //E0304 no instance of overloaded function "getline" matches the argument list
*/
    engine->Play2D(filename); //E0304 no instance of overloaded function "irrklang::ISoundEngine::play2D" matches the argument list


}

I tried looking everywhere. There is no thread asking the same question as I am. I could not find any way to convert string into ISoundSource*. I have read the entire documentation and there is no mention of using a string variable as input anywhere. I'd need to embed the file path in the code itself for it to work.

Any help would be greatly appreciated!


Solution

  • I could not find any way to convert string into ISoundSource.*
    You don't need to convert a std::string into an ISoundSource. Instead there is an overload of Play2D that takes a const char*:

    virtual ISound* irrklang::ISoundEngine::play2D  (   const char *    soundFileName,
    bool    playLooped = false,
    bool    startPaused = false,
    bool    track = false,
    E_STREAM_MODE   streamMode = ESM_AUTO_DETECT,
    bool    enableSoundEffects = false   
    )   
    

    So to use this use the string::c_str() function to get a const char* from the std::string and so your call becomes:

    engine->Play2D(filename.c_str());