WAV文件停止功能



我需要在项目中实现一些声音。我设法执行了播放功能,但是我还需要一个可以在需要时停止文件的功能。我在下面具有停止功能,这与播放功能类似。

关于为什么不起作用的任何提示,或有关如何在文件结束之前停止的一些替代方案。

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

您的代码似乎不是问题。

我的想法是在停止功能中添加判断条件。

判断是否有必要停止比赛,如果有必要停止比赛,按'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);
    }
}

这是最简单的代码演示。

更新:

LRESULT CALLBACK ButtonProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    TCHAR szText[40];
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_PAINT:
        GetClientRect(hwndDlg, &rect);
        GetWindowText(hwndDlg, szText, sizeof(szText));
        hdc = BeginPaint(hwndDlg, &ps);
        Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
        DrawText(hdc, szText, -1, &rect,
            DT_SINGLELINE | DT_CENTER | DT_VCENTER);
        return 0;
    case WM_LBUTTONDOWN:
    {
        mciSendString(L"open C:\Users\strives\Desktop\Ring10.wav type waveaudio alias Current_Sound_Command", NULL, 0, NULL);
        mciSendString(L"play Current_Sound_Command", NULL, 0, NULL);
    }
    break;
    case WM_LBUTTONUP:
    {
        mciSendString(L"stop Current_Sound_Command", NULL, 0, NULL);
    }
    break;
    default:
        return DefWindowProc(hwndDlg, message, wParam, lParam);
    }
    return 0;
}

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

最新更新