我想实现按下一个键就能发出一个声音,调用的是beep函数,但是我给每个beep都开了一个线程但是他们还是一个音接着另一个音播放没有同时进行
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "resource.h"
HANDLE outputhandle = 0;
HINSTANCE Inst = 0;
void OnSize(HWND hWnd, LPARAM lParam)
{
short width = LOWORD(lParam);
short hight = HIWORD(lParam);
char str[100];
sprintf(str, "width = %d, hight = %d", width, hight);
WriteConsole(outputhandle, str, strlen(str), NULL, NULL);
}
//发声函数
DWORD CALLBACK soundProc(LPVOID pParam)
{
WPARAM wParam = (WPARAM)pParam;
char str[40];
sprintf(str, "Press key%d \n", (int)wParam);
//Sleep(5000);
WriteConsole(outputhandle, str, strlen(str), NULL, NULL);
int r = rand() % 2000 + 300;
Beep(r, 200);
ExitThread(0);
return 0;
}
//一按键就开一个线程
void OnKeyDown(HWND hWnd, WPARAM wParam)
{
DWORD nID;
HANDLE hThread = CreateThread(NULL, 1, soundProc, (void*)wParam, 0, &nID);
return;
}
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msgID, WPARAM wParam, LPARAM lParam)
{
switch (msgID)
{
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE)
EndDialog(hWndDlg, 0);
}
return FALSE;
}
void OnCommand(HWND hWnd, WPARAM wParam)
{
switch (LOWORD(wParam))
{
case ID_MOD:
DialogBox(Inst, (char*)IDD_DIALOG1, hWnd, DlgProc);
break;
}
}
//处理窗口消息 窗口句柄 消息ID 附带参数
LRESULT APIENTRY WindProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM IParam)
{
switch (msgID)
{
/*case WM_SIZE:
OnSize(hWnd, IParam);
break;*/
case WM_COMMAND:
OnCommand(hWnd, wParam);
break;
case WM_KEYDOWN:
OnKeyDown(hWnd, wParam);
break;
case WM_SYSKEYDOWN:
OnKeyDown(hWnd, wParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE)
{
int ret = MessageBox(hWnd, "是否退出", "退出", MB_YESNO);
if (ret == IDYES)
break;
else
return 0;
}
break;
}
return DefWindowProc(hWnd, msgID, wParam, IParam);
}
//入口函数 本进程的句柄 上一个进程的句柄 cmd命令行参数 窗口展示形式
int WINAPI WinMain( HINSTANCE hIns, HINSTANCE hPreIns, LPSTR lpCmdline, int nCmdShow)
{
//分配一个cmd
AllocConsole();
outputhandle = GetStdHandle(STD_OUTPUT_HANDLE);
Inst = hIns;
//注册窗口类
WNDCLASS wc = { 0 };
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = NULL;
wc.hIcon = NULL;
wc.hInstance = hIns;
wc.lpfnWndProc = WindProc;
wc.lpszClassName = "Main";
//wc.lpszMenuName = (char*)IDR_MENU1;_In_opt;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
HMENU hMenu = LoadMenu(hIns, (char*)IDR_MENU1);
RegisterClass(&wc);
HWND hWnd = CreateWindowExW(0, L"Main", L"windows", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, hMenu, hIns, NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
MSG nMsg = { 0 };
while (1)
{
if (PeekMessage(&nMsg, NULL, 0, 0, PM_NOREMOVE))
{
if (GetMessage(&nMsg, NULL, 0, 0))
{
TranslateMessage(&nMsg);
DispatchMessageW(&nMsg);
}
else
return 0;
}
}
return 0;
}
PlaySound
The PlaySound function plays a sound specified by the given filename, resource, or system event. (A system event may be associated with a sound in the registry or in the WIN.INI file.)
BOOL PlaySound(
LPCSTR pszSound,
HMODULE hmod,
DWORD fdwSound
);
Parameters
pszSound
A string that specifies the sound to play. If this parameter is NULL, any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE in the fdwSound parameter.
Three flags in fdwSound (SND_ALIAS, SND_FILENAME, and SND_RESOURCE) determine whether the name is interpreted as an alias for a system event, a filename, or a resource identifier. If none of these flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the specified sound name. If an association is found, the sound event is played. If no association is found in the registry, the name is interpreted as a filename.
hmod
Handle of the executable file that contains the resource to be loaded. This parameter must be NULL unless SND_RESOURCE is specified in fdwSound.
fdwSound
Flags for playing the sound. The following values are defined:
SND_APPLICATION
The sound is played using an application-specific association.
SND_ALIAS
The pszSound parameter is a system-event alias in the registry or the WIN.INI file. Do not use with either SND_FILENAME or SND_RESOURCE.
SND_ALIAS_ID
The pszSound parameter is a predefined sound identifier.
SND_ASYNC
The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.
SND_FILENAME
The pszSound parameter is a filename.
SND_LOOP
The sound plays repeatedly until PlaySound is called again with the pszSound parameter set to NULL. You must also specify the SND_ASYNC flag to indicate an asynchronous sound event.
SND_MEMORY
A sound event's file is loaded in RAM. The parameter specified by pszSound must point to an image of a sound in memory.
SND_NODEFAULT
No default sound event is used. If the sound cannot be found, PlaySound returns silently without playing the default sound.
SND_NOSTOP
The specified sound event will yield to another sound event that is already playing. If a sound cannot be played because the resource needed to generate that sound is busy playing another sound, the function immediately returns FALSE without playing the requested sound.
If this flag is not specified, PlaySound attempts to stop the currently playing sound so that the device can be used to play the new sound.
SND_NOWAIT
If the driver is busy, return immediately without playing the sound.
SND_PURGE
Sounds are to be stopped for the calling task. If pszSound is not NULL, all instances of the specified sound are stopped. If pszSound is NULL, all sounds that are playing on behalf of the calling task are stopped.
You must also specify the instance handle to stop SND_RESOURCE events.
SND_RESOURCE
The pszSound parameter is a resource identifier; hmod must identify the instance that contains the resource.
SND_SYNC
Synchronous playback of a sound event. PlaySound returns after the sound event completes.
Return Values
Returns TRUE if successful or FALSE otherwise.
Remarks
The sound specified by pszSound must fit into available physical memory and be playable by an installed waveform-audio device driver. PlaySound searches the following directories for sound files: the current directory; the Windows directory; the Windows system directory; directories listed in the PATH environment variable; and the list of directories mapped in a network. For more information about the directory search order, see the documentation for the OpenFile function.
If it cannot find the specified sound, PlaySound uses the default system event sound entry instead. If the function can find neither the system default entry nor the default sound, it makes no sound and returns FALSE.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in mmsystem.h.
Import Library: Use winmm.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.
See Also
Waveform Audio Overview, Waveform Functions
playsound代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<Mmsystem.h>
#pragma comment(lib,"winmm.lib")
#include "resource.h"
const int mod = 28;
HANDLE outputhandle = 0;
HINSTANCE Inst = 0;
char keyname[88][5] = {
//1 2 3 4 5 6 7
//1 2 3 4 5 6 7 8 9 10 11 12
"A0", "#A0", "B0", //大字二组
"C1", "#C1", "D1", "#D1", "E1", "F1", "#F1", "G1", "#G1", "A1", "#A1", "B1", //大字一组
"C2", "#C2", "D2", "#D2", "E2", "F2", "#F2", "G2", "#G2", "A2", "#A2", "B2", //大字组
"C3", "#C3", "D3", "#D3", "E3", "F3", "#F3", "G3", "#G3", "A3", "#A3", "B3", //小字组
"C4", "#C4", "D4", "#D4", "E4", "F4", "#F4", "G4", "#G4", "A4", "#A4", "B4", //小字一组
"C5", "#C5", "D5", "#D5", "E5", "F5", "#F5", "G5", "#G5", "A5", "#A5", "B5", //小字二组
"C6", "#C6", "D6", "#D6", "E6", "F6", "#F6", "G6", "#G6", "A6", "#A6", "B6", //小字三组
"C7", "#C7", "D7", "#D7", "E7", "F7", "#F7", "G7", "#G7", "A7", "#A7", "B7", //小字四组
"C8", //小字五组
};
int littlestar[] = {
1, 1, 8, 8, 10, 10, 8,
6, 6, 5, 5, 3, 3, 1,
8, 8, 6, 6, 5, 5, 3,
8, 8, 6, 6, 5, 5, 3,
};
int presskeycnt = 0;
void OnSize(HWND hWnd, LPARAM lParam)
{
short width = LOWORD(lParam);
short hight = HIWORD(lParam);
char str[100];
sprintf(str, "width = %d, hight = %d", width, hight);
WriteConsole(outputhandle, str, strlen(str), NULL, NULL);
}
DWORD CALLBACK soundProc(LPVOID pParam)
{
char filename[20];
sprintf(filename, "piano/%s.wav", keyname[littlestar[presskeycnt % mod] + 38]);
PlaySound(filename, NULL, SND_FILENAME | SND_ASYNC);
ExitThread(0);;
return 0;
}
void OnKeyDown(HWND hWnd, WPARAM wParam)
{
DWORD nID;
char str[40];
presskeycnt++;
HANDLE hThread = CreateThread(NULL, 1, soundProc, (void*)wParam, 0, &nID);
sprintf(str, "Press key%d \n", (int)wParam);
WriteConsole(outputhandle, str, strlen(str), NULL, NULL);
return;
}
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msgID, WPARAM wParam, LPARAM lParam)
{
switch (msgID)
{
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE)
EndDialog(hWndDlg, 0);
}
return FALSE;
}
void OnCommand(HWND hWnd, WPARAM wParam)
{
switch (LOWORD(wParam))
{
case ID_MOD:
DialogBox(Inst, (char*)IDD_DIALOG1, hWnd, DlgProc);
break;
}
}
//处理窗口消息 窗口句柄 消息ID 附带参数
LRESULT APIENTRY WindProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM IParam)
{
switch (msgID)
{
/*case WM_SIZE:
OnSize(hWnd, IParam);
break;*/
case WM_COMMAND:
OnCommand(hWnd, wParam);
break;
case WM_KEYDOWN:
OnKeyDown(hWnd, wParam);
break;
case WM_SYSKEYDOWN:
OnKeyDown(hWnd, wParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE)
{
int ret = MessageBox(hWnd, "是否退出", "退出", MB_YESNO);
if (ret == IDYES)
break;
else
return 0;
}
break;
}
return DefWindowProc(hWnd, msgID, wParam, IParam);
}
//入口函数 本进程的句柄 上一个进程的句柄 cmd命令行参数 窗口展示形式
int WINAPI WinMain( HINSTANCE hIns, HINSTANCE hPreIns, LPSTR lpCmdline, int nCmdShow)
{
//分配一个cmd
AllocConsole();
outputhandle = GetStdHandle(STD_OUTPUT_HANDLE);
Inst = hIns;
//注册窗口类
WNDCLASS wc = { 0 };
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = NULL;
wc.hIcon = NULL;
wc.hInstance = hIns;
wc.lpfnWndProc = WindProc;
wc.lpszClassName = "Main";
//wc.lpszMenuName = (char*)IDR_MENU1;_In_opt;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
HMENU hMenu = LoadMenu(hIns, (char*)IDR_MENU1);
RegisterClass(&wc);
HWND hWnd = CreateWindowExW(0, L"Main", L"windows", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, hMenu, hIns, NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
MSG nMsg = { 0 };
while (1)
{
if (PeekMessage(&nMsg, NULL, 0, 0, PM_NOREMOVE))
{
if (GetMessage(&nMsg, NULL, 0, 0))
{
TranslateMessage(&nMsg);
DispatchMessageW(&nMsg);
}
else
return 0;
}
}
return 0;
}