PlaySound is a unique API function of the Windows platform. It only supports the playback of audio files in .wav format. The library file used is winmm.lib. This library file is a library file that comes with Windows. It only needs to be declared, and there is no need to add it manually. .
- The statement is as follows:
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
- It is divided into synchronous mode and asynchronous mode.
- 1) Synchronization method
//1) MBCS编码
PlaySound("hello.wav", NULL, SND_SYNC | SND_FILENAME | SND_NOSTOP);
//2) Unicode编码
PlaySound(L"hello.wav", NULL, SND_SYNC | SND_FILENAME | SND_NOSTOP);
//3) 兼容MBCS编码与Unicode编码
PlaySound(TEXT("hello.wav"), NULL, SND_SYNC | SND_FILENAME | SND_NOSTOP);
- 2) Asynchronous way
//1) MBCS编码
PlaySound("hello.wav", NULL, SND_ASYNC | SND_FILENAME | SND_NOSTOP);
//2) Unicode编码
PlaySound(L"hello.wav", NULL, SND_ASYNC | SND_FILENAME | SND_NOSTOP);
//3) 兼容MBCS编码与Unicode编码
PlaySound(TEXT("hello.wav"), NULL, SND_ASYNC | SND_FILENAME | SND_NOSTOP);
Here we take a console application DoPlay as an example to illustrate, the development tool used is Visual Studio 2005,
click [File] on the Visual Studio toolbar -- "New --" Visual C++ -- "Win32 -- "Win32 console application Program--"Name: DoPaly
1 synchronous playback sync
1.1 Using multibyte character set (MBCS) encoding
//DoPlay.cpp
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound("hello.wav", NULL, SND_SYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
1.2 Use Unicode character set encoding
//DoPlay.cpp
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound(L"hello.wav", NULL, SND_SYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
1.3 Compatible with MBCS and Unicode
//DoPlay.cpp
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound(TEXT("hello.wav"), NULL, SND_SYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
2 Asynchronous playback Async
2.1 Using multibyte character set (MBCS) encoding
//DoPlay.cpp
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound("hello.wav", NULL,SND_ASYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
2.2 Use Unicode character set encoding
//DoPlay.cpp
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound(L"hello.wav", NULL,SND_ASYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
2.3 Compatible with MBCS and Unicode
//DoPlay.cpp
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
int main()
{
PlaySound(TEXT("hello.wav"), NULL, SND_ASYNC |SND_FILENAME | SND_NOSTOP);
system("pause");
return 0;
}
3 Appendix
- Since the PlaySound() function only supports the playback of audio files in the .wav format, the supported formats are too single.
- To support playback of audio files such as mp3, ogg, flac, oga, ac3, aac, wav, etc., please use the mciSendString() function
function | audio format |
---|---|
PlaySound() | Only supports .wav audio format |
mciSendString() | Support mp3, ogg, flac, oga, ac3, aac, wavc and other audio formats |
- The function declaration of mciSendString is as follows:
BOOL mciSendString(“Command1 FILE Command2”,NULL,0,NULL);
The three variables in the formal parameter "Command1 FILE Command2" are in the relationship of logic &&, and the possible combinations are as follows:
- “Command1 FILE”
- “Command1 FILE Command2”
2) The header files required by the mciSendString function are as follows:
#include<windows.h>
#pragma comment(lib,"winmm.lib")
3.1 Play once
//DoMusic.cpp
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
int main()
{
mciSendString(TEXT("play .\\World.mp3"), NULL, 0, NULL);
system("pause");
return 0;
}
3.2 Repeat playback
//DoMusic.cpp
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
int main()
{
mciSendString(TEXT("play .\\World.mp3 repeat"), NULL, 0, NULL);
system("pause");
return 0;
}
3.3 End playback
//DoMusic.cpp
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
int main()
{
mciSendString(TEXT("close .\\World.mp3 repeat"), NULL, 0, NULL);
system("pause");
return 0;
}