How Do I Get mciSendString to Work With Both Variables and Spaces?

1.1k views Asked by At

I have created a program that plays music in my music directory and supports spaces, thanks to the help of Google. However, I don't want to keep hard-coding the song names, so I'm wondering if I can replace the file name with a variable, and std::cin the variable when my program is run. However, I also want to be able play songs that have spaces in their names. I made a wrapper function (I think that this would count as one?) for mciSendString, and I'm trying to be able to pass a "name" parameter to it for it to play. However, due to complications with LPCWSTR and std::string, among other things, I'm having difficulty implementing this.

Here's my current music function:

 void Music(std::string strSongName,DWORD dwExtraFlag)
{

    MCIERROR me =
        mciSendString(TEXT("open \"D:\\Windows.old\\Users\\MyName\\Desktop\\Sounds\\Music\\song name.mp3\""" type mpegvideo alias song1"),
            NULL, 0, 0);

/*
note that I want to be able to play songs 
with spaces in their name
*/

    if (me == 0)
    {
        me = mciSendString(TEXT("play song1 wait repeat"), NULL, 0, 0);
        mciSendString(TEXT("close song1"), NULL, 0, 0);
    }
}

What I want it to be like:

 void Music(std::string strName,DWORD dwExtraFlag)
{

    MCIERROR me =
        mciSendString(TEXT("open \"D:\\Windows.old\\Users\\MyName\\Desktop\\Sounds\\Music\\STRNAMEVARIABLEHERE.mp3\""" type mpegvideo alias song1"),
            NULL, 0, 0);

/*
note that I want to be able to play songs 
with spaces in their name
*/

    if (me == 0)
    {
        me = mciSendString(TEXT("play song1 wait repeat"), NULL, 0, 0);
        mciSendString(TEXT("close song1"), NULL, 0, 0);
    }
}

I would then make a variable in main or some other function and std::cin into that variable.

std::string strNameOfSong = "";
std::cin >> strNameOfSong;

Then, I would call music like:

Music(strNameOfSong,0)

I would also like to mention that you can ignore the "dwExtraFlags" argument if you wish, it is something that I have yet to implement.

Anyways, I would really appreciate help implementing this as I am new to C++. However, I don't want to leave this project unfinished, and I wish to complete it before moving on.

I would greatly appreciate any solutions, help, or tips. If you want me to be more specific or provide any more information to help you formulate an answer, I would be glad to do so.

Thank you very much.

EDIT: I implemented Ethan's suggestion. After adding some things in and adding his suggestion, my program stops working shortly after typing in the name of the song I want. I've tried debugging and it reports:

"Exception thrown at 0x00047430 in Program.exe: 0xC0000005: Access violation writing location 0x6563696E."

Here's what I added into my main function (aside from Ethan's suggestion):

  static std::string strSongName = "";
    std::cin >> strSongName;
    CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&Music, &strSongName, 0, 0);
    // The above is to createthread so I can play music while doing other things

Debugging, the values seem to be correct. However, I suddenly get that exception and the application stops working. How can I have an access violation?

Another small change that I made to Ethan's suggestion:

MCIERROR me =
        mciSendString((LPCWSTR)(songFile.c_str()), NULL, 0, 0);

(I typecasted SongFile as it was giving me errors)

I also changed the example of the path back to normal.

EDIT2:

Fixed the exception! It was because I (in a silly manner), forgot to dereference the pointer to "strSongName". However, anything after the space still isn't recognized, it seems, as I debugged it. It also doesn't seem to play songs even when they do not have a space, and yes I am adding .mp3 too.

1

There are 1 answers

2
Ethan F. On

How about this one?

void Music(std::string strName,DWORD dwExtraFlag)
{
std::string songFile = "open \"D:\\Windows.old\\Users\\MyName\\Desktop\\Sounds\\Music\\";
songFile += strName + "\" type mpegvideo alias song1";

MCIERROR me =
    mciSendString(songFile.c_str(), NULL, 0, 0);

/*
note that I want to be able to play songs 
with spaces in their name
*/

if (me == 0)
{
    me = mciSendString(TEXT("play song1 wait repeat"), NULL, 0, 0);
    mciSendString(TEXT("close song1"), NULL, 0, 0);
}
}