-1

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!

Tetrapak
  • 21
  • 6
  • I did not downvote however from years of experience on this site I expect you got downvoted because your question did not show that you made an effort to read the documentation and try to solve the problem yourself. One overload of the [Play2D function](https://www.ambiera.com/irrklang/docu/classirrklang_1_1_i_sound_engine.html#a25f612fe6479d3b22dc5bab2a2eaa927) takes a const char* filename which you can easily get from the [string](https://en.cppreference.com/w/cpp/string/basic_string/c_str) if you have basic c++ experience. – drescherjm May 18 '23 at 17:30
  • I am quite new to C++. I came from C# and am trying to recreate my program in C++. Sorry – Tetrapak May 18 '23 at 17:49
  • 1
    Try: `engine->Play2D(filename.c_str());` – drescherjm May 18 '23 at 17:51
  • @drescherjm I have actually just found that. I had to debug some other lines before being able to test it though. Please write this as an answer so I can select it. – Tetrapak May 18 '23 at 18:12

1 Answers1

1

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());
drescherjm
  • 10,365
  • 5
  • 44
  • 64