简体   繁体   中英

C++ not playing mp3 files through mciSendString

I am aiming to build an audio player that plays MP3 files. For that, I have used the mciSendString() function. All the MP3 files are in the same folder as the main source file. I have looked at the documentation and some syntax online, despite that I am unable to play the MP3 files. When I select a song, it doesn't play, and the code skips to system("pause") .

My header files

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

using namespace std;

Function that plays the MP3 files:

void playsong()
{
    int song;
    system("cls");
    cout << "****************************" << endl;
    cout << "\tPLAYING SONG\n";
    cout << "****************************" << endl;
    cout << "List of Songs\n";
    cout << "1.0\n";
    cout << "2.AFSANAY\n";
    cout << "3.Agency\n";
       
    cin >> song;
    switch(song)
    {
        case 1:
        {
            mciSendString("open \"C:\\Users\Murad\Documents\3rd Semester\OOP\Assignments\Assignmnet 1\Assignmnet 1\0.mp3\" type mpegvideo alias mp3", NULL, 0, NULL);
            mciSendString("play mp3", NULL, 0, NULL); 
            break;
        }
        case 2:
        {
           mciSendString("open \"C:\\Users\Murad\Documents\3rd Semester\OOP\Assignments\Assignmnet 1\Assignmnet 1\AFSANAY.mp3\" type mpegvideo alias mp3", NULL, 0, NULL);
           mciSendString("play mp3", NULL, 0, NULL);
           break;
        }
        case 3:
        {
            mciSendString("open \"C:\\Users\Murad\Documents\3rd Semester\OOP\Assignments\Assignmnet 1\Assignmnet 1\Agency.mp3\" type mpegvideo alias mp3", NULL, 0, NULL);
            mciSendString("play mp3", NULL, 0, NULL);
            break;
        }
    }

    system("pause");
    system("cls");
    display();
}

Your open commands are not escaped correctly, and you are not checking return values for errors.

But even if no errors were occurring, the play command is asynchronous , so mciSendString() will still exit immediately once playback begins. You need to specify the wait flag to wait for playback to finish, or else use the notify flag to be notified when playback is finished. See The Wait, Notify, and Test Flags

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM