简体   繁体   English

如何使mciSendString与变量和空格一起使用?

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

I have created a program that plays music in my music directory and supports spaces, thanks to the help of Google. 感谢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. 但是,我不想继续对歌曲名称进行硬编码,所以我想知道是否可以在运行程序时用一个变量替换文件名,并使用std :: cin替换该变量。 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. 我为mciSendString做了一个包装函数(我认为这算作一个吗?),并且我试图能够向它传递一个“名称”参数以使其发挥作用。 However, due to complications with LPCWSTR and std::string, among other things, I'm having difficulty implementing this. 但是,由于LPCWSTR和std :: string的复杂性,我很难实现这一点。

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. 然后,我将在main或某些其他函数中将一个变量和std :: cin制成该变量。

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. 我还要提及的是,如果您愿意,可以忽略“ dwExtraFlags”参数,这是我尚未实现的。

Anyways, I would really appreciate help implementing this as I am new to C++. 无论如何,由于我是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." “在Program.exe中的0x00047430处引发异常:0xC0000005:访问冲突写入位置0x6563696E。

Here's what I added into my main function (aside from Ethan's suggestion): 这是我添加到主要函数中的内容(除了Ethan的建议):

  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: 我对Ethan的建议进行的另一个小更改:

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

(I typecasted SongFile as it was giving me errors) (我播出了SongFile,因为它给了我错误)

I also changed the example of the path back to normal. 我也将路径示例恢复为正常。

EDIT2: 编辑2:

Fixed the exception! 修复了异常! It was because I (in a silly manner), forgot to dereference the pointer to "strSongName". 这是因为我(愚蠢地)忘记了取消对指向“ 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. 即使它们没有空格,它似乎也不会播放歌曲,是的,我也要添加.mp3。

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);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM