I need to implement some sounds in a project. I managed to do the play part, but I also need a function that can stop the file whenever it is needed. I have the Stop function below, which is similar with the play function.
Any tips on why is not working, or some alternatives on how I can stop the file before it's ending.
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);
}
}
Your code doesn't seem to be a problem.
My idea is to add a judgment condition to the stop function.
By judging whether it is necessary to stop playing, if it is necessary to stop playing, press'y'
or continue playing.
This is just a simple idea. If you have any other questions, please feel free to reply to me.
#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);
}
}
This is the simplest code demonstration for reference only.
Updated:
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;
}
Customize the callback function of the button, set the mouse event of the button, press the button, play the file, release the button, stop playing.