First of all, write the following code:
#include <Mmsystem.h> #include <mciapi.h> //these two headers are already included in the <Windows.h> header #pragma comment(lib, "Winmm.lib")
Open * .mp3:
mciSendString("open \"*.mp3\" type mpegvideo alias mp3", NULL, 0, NULL);
To play * .mp3:
mciSendString("play mp3", NULL, 0, NULL);
To play and wait for the * .mp3 to finish playing:
mciSendString("play mp3 wait", NULL, 0, NULL);
To play (repeat playback from the beginning) * .mp3:
mciSendString("play mp3 from 0", NULL, 0, NULL);
To play and wait for the * .mp3 to finish playing:
mciSendString("play mp3 from 0 wait", NULL, 0, NULL);
To play * .mp3 and play it every time it ends as a loop:
mciSendString("play mp3 repeat", NULL, 0, NULL);
If you want to do something when * .mp3 has finished playing, you need RegisterClassEx on the structure of WNDCLASSEX , CreateWindowEx and process its messages using GetMessage TranslateMessage and DispatchMessage in a while and call:
mciSendString("play mp3 notify", NULL, 0, hwnd);
In the window procedure, add case MM_MCINOTIFY: The code will be executed there when mp3 finishes playing.
But , if you are programming the Console application and you are not dealing with windows , you can CreateThread in a paused state by specifying the CREATE_SUSPENDED flag in the dwCreationFlags parameter and save the return value in the static variable and call it dwCreationFlags you want. For example, I call it mp3. The type of this variable is static , of course, HANDLE .
Here is ThreadProc for lpStartAddress this thread:
DWORD WINAPI MP3Proc(_In_ LPVOID lpParameter) //lpParameter can be a pointer to a structure that store data that you cannot access outside of this function. You can prepare this structure before `CreateThread` and give it address in the `lpParameter` { Data *data = (Data*)lpParameter; //If you call this structure Data, but you can call it whatever you want. while (true) { mciSendString("play mp3 from 0 wait", NULL, 0, NULL); //Do here what you want to do when the mp3 playback is over SuspendThread(GetCurrentThread()); //or the handle of this thread that you keep in a static variable instead } }
All you have to do is ResumeThread(mp3); every time you want to play your mp3, and something will happen every time it ends.
You can #define play_my_mp3 ResumeThread(mp3); make the code more readable.
Of course, you can remove the while (true) , SuspendThread and 0 codes if you want to play only the mp3 file once and do whatever you want when it is over.
If you just delete the SuspendThread call, then the sound will play again and again and do something when it ends. This is equivalent to:
mciSendString("play mp3 repeat notify", NULL, 0, hwnd);
in the windows.
To pause * .mp3 in the middle:
mciSendString("pause mp3", NULL, 0, NULL);
and renew it:
mciSendString("resume mp3", NULL, 0, NULL);
To stop it in the middle:
mciSendString("stop mp3", NULL, 0, NULL);
Please note that you cannot resume a sound that was stopped, but only paused, but you can play it by running the play command. When you finish playing this * .mp3, don't forget:
mciSendString("close mp3", NULL, 0, NULL);
All these actions also apply to (working with) wave files, but with wave files you can use "waveaudio" instead of "mpegvideo". You can also just play them directly without opening them:
PlaySound("*.wav", GetModuleHandle(NULL), SND_FILENAME);
If you do not want to specify a module descriptor:
sndPlaySound("*.wav", SND_FILENAME);
If you do not want to wait for the end of playback:
PlaySound("*.wav", GetModuleHandle(NULL), SND_FILENAME | SND_ASYNC); //or sndPlaySound("*.wav", SND_FILENAME | SND_ASYNC);
Repeat wave file playback:
PlaySound("*.wav", GetModuleHandle(NULL), SND_FILENAME | SND_ASYNC | SND_LOOP); //or sndPlaySound("*.wav", SND_FILENAME | SND_ASYNC | SND_LOOP);
Please note that you must specify the flags SND_ASYNC and SND_LOOP , because you will never wait until the sound that is repeated countless times ends!
You can also fopen create a wave file and copy all its bytes to the buffer (huge / huge (very large) byte array) using the fread function, and then:
PlaySound(buffer, GetModuleHandle(NULL), SND_MEMORY); //or PlaySound(buffer, GetModuleHandle(NULL), SND_MEMORY | SND_ASYNC); //or PlaySound(buffer, GetModuleHandle(NULL), SND_MEMORY | SND_ASYNC | SND_LOOP); //or sndPlaySound(buffer, SND_MEMORY); //or sndPlaySound(buffer, SND_MEMORY | SND_ASYNC); //or sndPlaySound(buffer, SND_MEMORY | SND_ASYNC | SND_LOOP);
Either OpenFile , or CreateFile or CreateFile2 , and the ReadFile or ReadFileEx can be used instead of the fopen and fread functions.
Hope this fully answers your question.