Using mciSendString to play an mp3 file and not working (and also not giving any error)

521 views Asked by At

Here is my code:

#include <Windows.h>
#include <iostream>
#pragma comment(lib, "Winmm.lib")

int main() {
    mciSendString("open \"*.mp3\" type mpegvideo alias mp3", NULL, 0, NULL);
    mciSendString("play mp3", NULL, 0, NULL);

    return 0;
}

When I do replace the "*.mp3" with the file path to my mp3 file and run my program, there is no audio that plays and the program end immediately, what am I doing wrong? There is no error it just ends without playing anything. Is there something I am missing or doing wrong? It might also be important to mention that I am using VC++.

1

There are 1 answers

0
AudioBubble On

mciSendString doesn't actually cause your program to wait, so you would need to wait instead of ending the program. Alternatively, you can use PlaySound() and specify the SND_SYNC flag so that it waits for the file to finish playing.