简体   繁体   English

WAV文件停止功能

[英]Wav file stop function

I need to implement some sounds in a project,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, wich 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. 通过判断是否有必要停止播放,是否有必要停止播放,按'y'或继续播放。

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. 自定义按钮的回调功能,设置按钮的鼠标事件,按下按钮,播放文件,释放按钮,停止播放。

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

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