MP3 playing using mci send string c++

user2114036 picture user2114036 · Mar 19, 2013 · Viewed 8k times · Source

I'm trying to play some mp3 files as my background music in one of my project which Im doing. I tried to play it using mcisendstring but it just couldnt work :(

These what I have done:

CMP3_MCI myMp3;
std::string address= "C:\\Users\\music embed testing\\test.mp3";
myMp3.Load(address);
myMp3.Play();

//Load function

void Load(string szFileName)
{
    m_szFileName = szFileName;
    Load2();
}

//load2 function

void Load2()
{
      std::string szCommand = "open \"" + GetFileName() + "\" type mpegvideo alias " + GetFileName();       
       mciSendString(szCommand.c_str(), NULL, 0, 0);
}

//play function

void Play()
{
    std::string szCommand = "play " + GetFileName() + " from 0";
    mciSendString(szCommand.c_str(), NULL, 0, 0);
}

//getFileName basically returns m_szFileName stored as private attribute

Answer

user2114036 picture user2114036 · Mar 19, 2013

After much trial and error, I finally found out a way to make it work. To those who are facing the same problem as me, here it goes:

//if you are using unicode
LPCWSTR a = L"open cannon.mp3 type mpegvideo";
int error = 99;
error = mciSendString(a, NULL,0,0);
int error2;
LPCWSTR b = L"play cannon.mp3";
error2 = mciSendString(b, NULL, 0, 0);

//cannon.mp3 is stored in my resource file
//error is just for debugging

//if you are using multibyte

LPCSTR a = "open cannon.mp3 type mpegvideo";
mciSendString(a, NULL, 0,0);
LPCSTR b = "play cannon.mp3 repeat";
int error2 = mciSendString(b, NULL, 0, 0);