2

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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2114036
  • 153
  • 3
  • 13
  • Isn't your alias more than one word? Does that work? I don't really see the use of an alias here, anyway. – chris Mar 19 '13 at 02:33
  • Hi, Im new to using mciSendString so basically I tried to copy one of those functions out there. Im not exactly sure what are they doing though..what the use of alias and how do we omit it? For eg: http://www.codeproject.com/Articles/17279/Using-mciSendString-to-play-media-files – user2114036 Mar 19 '13 at 02:39
  • I'd say just use `"open \"" + GetFileName() + "\" type mpegvideo` and then use `"\" + GetFileName() + "\"` for the file. – chris Mar 19 '13 at 02:44
  • Doesnt work:( any idea how to check if the command mcisendstring has successfully load/play the music file? – user2114036 Mar 19 '13 at 04:05
  • One or two of the parameters is for error checking. The documentation has details on that. – chris Mar 19 '13 at 04:12
  • Okay got it. My mcisendstring for load () returns an error of 259 which means unrecognised keyword according to msdn. Any idea? – user2114036 Mar 19 '13 at 04:26
  • Nothing I can see. I don't use this all too often, but it looks ok. You can probably leave out the "from 0" on the play string as well, as it defaults to the beginning if you haven't paused it somewhere. – chris Mar 19 '13 at 04:35

1 Answers1

4

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);
user2114036
  • 153
  • 3
  • 13