C++ Wav文件停止功能

C++ Wav文件停止功能,c++,winapi,wav,C++,Winapi,Wav,我需要在项目中实现一些声音,设法完成播放部分,但我还需要一个函数,可以在需要时停止文件。 我有下面的停止功能,它与播放功能类似 关于为什么不工作的任何提示,或者关于如何在文件结束前停止文件的一些替代方法 LPCSTR const Sound_File_Open = "open C:/Users/uidn1646/Desktop/sound/1162.wav alias Current_Sound_Command"; void Stop() { LPCSTR const Sound_Co

我需要在项目中实现一些声音,设法完成播放部分,但我还需要一个函数,可以在需要时停止文件。 我有下面的停止功能,它与播放功能类似

关于为什么不工作的任何提示,或者关于如何在文件结束前停止文件的一些替代方法

LPCSTR const Sound_File_Open = "open C:/Users/uidn1646/Desktop/sound/1162.wav alias Current_Sound_Command";

void Stop()
{
    LPCSTR const Sound_Command = "stop Current_Sound_Command ";

    MCIERROR sound_file_action = mciSendString(Sound_File_Open, NULL, 0, NULL);
    if (sound_file_action == 0) {
        mciSendString(Sound_Command, NULL, 0, NULL);
        mciSendString("close Current_Sound_Command", NULL, 0, NULL);

    }
}

你的代码似乎没有问题

我的想法是在stop函数中添加一个判断条件

通过判断是否需要停止播放,如果需要停止播放,请按
'y'
或继续播放

这只是一个简单的想法。如果您有任何其他问题,请随时回复我

#include <conio.h>
#include <Windows.h>
#include <iostream>

#pragma comment (lib,"Winmm.lib")
using namespace std;

LPCSTR const Sound_Command = "stop Current_Sound_Command";

void stop();

int main() 
{
    LPCSTR const Sound_File_Open = "open C:\\Users\\strives\\Desktop\\Ring10.wav type waveaudio alias Current_Sound_Command";       
    mciSendString(Sound_File_Open, NULL, 0, NULL); 
    mciSendString("play Current_Sound_Command", NULL, 0,  NULL);

    //here you need stop play
    stop();                         
    system("pause");
}

void stop()
{   
    char letter_1 = {'y'};
    cout << "Do you want to stop play?" << endl;
    cout << "y or n" << endl;
    char letter = _getch();
    if (letter == letter_1)
    {
        MCIERROR sound_file_action = mciSendString(Sound_Command, NULL, 0, NULL);
    }
}

自定义按钮的回调函数,设置按钮的鼠标事件,按下按钮,播放文件,释放按钮,停止播放。

您的代码似乎没有问题

我的想法是在stop函数中添加一个判断条件

通过判断是否需要停止播放,如果需要停止播放,请按
'y'
或继续播放

这只是一个简单的想法。如果您有任何其他问题,请随时回复我

#include <conio.h>
#include <Windows.h>
#include <iostream>

#pragma comment (lib,"Winmm.lib")
using namespace std;

LPCSTR const Sound_Command = "stop Current_Sound_Command";

void stop();

int main() 
{
    LPCSTR const Sound_File_Open = "open C:\\Users\\strives\\Desktop\\Ring10.wav type waveaudio alias Current_Sound_Command";       
    mciSendString(Sound_File_Open, NULL, 0, NULL); 
    mciSendString("play Current_Sound_Command", NULL, 0,  NULL);

    //here you need stop play
    stop();                         
    system("pause");
}

void stop()
{   
    char letter_1 = {'y'};
    cout << "Do you want to stop play?" << endl;
    cout << "y or n" << endl;
    char letter = _getch();
    if (letter == letter_1)
    {
        MCIERROR sound_file_action = mciSendString(Sound_Command, NULL, 0, NULL);
    }
}

自定义按钮的回调函数,设置按钮的鼠标事件,按下按钮,播放文件,释放按钮,停止播放。

在“停止当前声音”命令的末尾有一个额外的空间。。第二次和第三次
mciSendString
调用的结果未被检查,错误代码也未被检查。它不是空格,在play函数中有相同的空格,但同时删除了它们。。停止功能不工作。
“停止当前声音\u命令”
末尾有一个额外的空格。第二次和第三次
mciSendString
调用的结果未被检查,错误代码也未被检查。它不是空格,在play函数中有相同的空格,但同时删除了它们。。停止功能不起作用。我在应用程序中有类似的功能。长话短说,当模拟中的一个按钮被按下时,会有一个事件启动声音(称之为if语句的真端),当按钮被释放时,声音应该停止(假端)。这是我们用来测试东西的方法。@SilviuAlexandru您可以自定义鼠标左键事件。我更新了我的答案。我在应用程序中有类似的内容。长话短说,当模拟中的一个按钮被按下时,会有一个事件启动声音(称之为if语句的真实面),当按钮被释放时,声音应该停止(错误面)。这是我们用来测试东西的方法。@SilviuAlexandru您可以自定义鼠标左键事件。我更新了我的答案。