How to play a part of an MP3 file in C++

This tutorial is about how to play a part of an MP3 file in C++. We can write a program in C++ that would play an mp3 audio file using the mciSendString() function. Let’s understand how to do it.

Play a specific part of an MP3 file in C++

Before we start writing the C++ program for playing a part of the mp3 file, we need to ensure some things such as:

  • The audio file, that we are going to play, must be saved in the same folder where your program is saved. If not, then you have to specify the proper path string in the MCI command string while opening the file.
  • Go to the project options or an equivalent tab in your compiler and type this in the linker section: “-lwinmm”. In Dev-C++, go to project->project options->parameters->linker section and then type the above.
  • Save your program with .cpp or .cp extensions.

Now we can start writing the program. In the program, we are going to use mciSendString() function. The syntax for this function is as follows:

MCIERROR mciSendString(  LPCTSTR lpszCommand,  LPTSTR  lpszReturnString,  UINT    cchReturn,  HANDLE  hwndCallback );

Read about it here.

lpszCommand is a command string. To open an audio file this string will be:

open \"Music.mp3\" type mpegvideo alias mp3

Here we have used an alias for the file “Music.mp3” which is mp3. We will use this alias (mp3) for further functioning with the file. This alias does not necessarily have to be mp3, you can choose any name of your choice.

Now to play a part of the file, the command string should be like this:

play mp3 from 10000 to 30000 wait

Here, ‘play mp3’ specifies that the file now has to be played and “from 10000” specifies the start position where the file should start playing and “to 30000” specifies the end position where the file should stop playing.

Now before the program ends we must close the audio file. The below lpszCommand string closes the file.

close mp3

The example program is given here.

#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;

int main()
  int n;
  //open the audio file
  mciSendString("open \"Music.mp3\" type mpegvideo alias mp3", NULL, 0, NULL);
  cout<<"Press 1 to start playing.\n";
  //play the audio file and specify start and end positions 
    cout<<"file playing....\n";
    time_t t1=time(0);
    mciSendString("play mp3 from 10000 to 30000 wait", NULL, 0, NULL);
    time_t t2=time(0);
    cout<<"Duration of the play :"<<t2-t1<<" seconds."<<endl;
    cout<<"You did not press 1."<<endl;
  //close the audio file
  mciSendString("close mp3", NULL, 0, NULL);
  return 0;


Press 1 to start playing.
file playing....
Duration of the play :14 seconds.

Note: The “to” position value must be greater than or equal to “from” position value and “to” position value cannot be greater than the end position of the file.

Thank you.

Also, read: How to find the duration or length of an MP3 File in C++

Leave a Reply

Your email address will not be published. Required fields are marked *