0

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();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Kh. Murad
  • 41
  • 7
  • 2
    One of the problems is the string that you have sent to `mciSendString`. Look up character escape sequences on Google – Asesh Jan 08 '22 at 07:05

1 Answers1

0

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770