#include
#include
#include
#include
#pragma comment(lib, "winmm.lib")
using namespace std;
DWORD WINAPI PlayMp3(LPVOID lpVoid)
{
LPCRITICAL_SECTION lpCriticalSection = (LPCRITICAL_SECTION)lpVoid;
EnterCriticalSection(lpCriticalSection);
char buf[128];
//use mciSendString()
//mciSendString("play e:\\songs\\把根留住.mp3",buf,sizeof(buf),NULL);
//mciSendString("play e:\\songs\\zhj.mp3",buf,sizeof(buf),NULL);
//char str[128] = {0};
//int i = 0;
//use mciSendCommand
MCI_OPEN_PARMS mciOpen;
MCIERROR mciError;
//mciOpen.lpstrDeviceType = (LPCTSTR)MCI_ALL_DEVICE_ID;
//mciOpen.lpstrDeviceType = "waveaudio"; //只能播放.wav文件
//mciOpen.lpstrDeviceType = "avivideo"; //*.avi
mciOpen.lpstrDeviceType = "mpegvideo";
//mciOpen.lpstrDeviceType = "sequencer";
mciOpen.lpstrElementName = "E:\\music\\新音乐\\梁静茹-我不快乐.wma";
//mciOpen.lpstrElementName = "e:\\movie\\first.avi";
//mciOpen.lpstrElementName = "c:\\winnt\\media\\Windows 登录音.wav";
mciError = mciSendCommand(0, MCI_OPEN,MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, (DWORD)&mciOpen);
if(mciError)
{
mciGetErrorString(mciError,buf,128);
//MessageBox("send MCI_PLAY command failed", "ERROR");
LeaveCriticalSection(lpCriticalSection);
return 0;
}
UINT DeviceID = mciOpen.wDeviceID ;
MCI_PLAY_PARMS mciPlay;
//mciError = mciSendCommand(DeviceID,MCI_PLAY,0 ,(DWORD)&mciPlay);
//MCI_FROM | MCI_TO |
mciError = mciSendCommand(DeviceID, MCI_PLAY, MCI_WAIT/* | MCI_DGV_PLAY_REPEAT*/,
(DWORD) (LPMCI_PLAY_PARMS)&mciPlay); //MCI_DGV_PLAY_REPEAT, 要 #include "Digitalv.h"
if(mciError)
{
mciGetErrorString(mciError, buf, 128);
//MessageBox("send MCI_PLAY command failed", "ERROR");
LeaveCriticalSection(lpCriticalSection);
return 0;
}
// while(1)
// {
// i++;
// Sleep(1000);
// }
LeaveCriticalSection(lpCriticalSection);
return 0;
}
DWORD WINAPI TestCriticalSection(LPVOID lpVoid)
{
LPCRITICAL_SECTION lpCriticalSection = (LPCRITICAL_SECTION)lpVoid;
// printf("sub must first Leave\n");
// LeaveCriticalSection(lpCriticalSection);
printf("subThread prepare enter\n");
EnterCriticalSection(lpCriticalSection);
printf("subThread already enter\n");
printf("subThread prepare leave\n");
LeaveCriticalSection(lpCriticalSection);
printf("subThread already leave\n");
return 0;
}
DWORD WINAPI EventFunc(LPVOID lpVoid)
{
HANDLE handleEvent = (HANDLE)lpVoid;
Sleep(3000);
printf("set the event");
SetEvent(handleEvent);
return 0;
}
int main(int argc, char *argv[])
{
CRITICAL_SECTION criticalSection;
InitializeCriticalSection(&criticalSection);
printf("Main thread enter the first time\n");
CreateThread(NULL, NULL, PlayMp3, &criticalSection, NULL, NULL);
Sleep(200);
EnterCriticalSection(&criticalSection);
LeaveCriticalSection(&criticalSection);
DeleteCriticalSection(&criticalSection);
/************************************************************************/
/* 测试事件Event */
/************************************************************************/
// HANDLE newHandle = CreateEvent(NULL, TRUE, TRUE,"newEvnet");
// ResetEvent(newHandle);
// CreateThread(NULL, NULL, EventFunc, newHandle, NULL, NULL);
// WaitForSingleObject(newHandle, INFINITE);
// ResetEvent(newHandle);
/************************************************************************/
/* 播放wav文件 */
/************************************************************************/
// char wavFile[MAX_PATH] = "D:\\wav\\Windows XP 启动.wav";
// sndPlaySound(wavFile, SND_SYNC);//函数在此阻塞 /* play synchronously (default) */
// //sndPlaySound(wavFile, SND_ASYNC);//好像是使用线程,非阻塞 /* play asynchronously */
// //Sleep(100000);
return 0;
}
/************************************************************************/
/*关于CRITICAL_SECTION */
// /************************************************************************/
// 1.主线程可以连续两次(多次)进入临界区,此时临界区的LockCount的计数是2,
// 子线程就有时可以进入临界区,有时不可以进入临界区
阅读(694) | 评论(0) | 转发(0) |