How to play and pause an MP3 file in C++

In this tutorial, we will learn how to play and pause an MP3 file in C++. We will use the mciSendString() function with the required multimedia command string for play and pause operation on the mp3 file. To know more about mciSendString() function and multimedia command strings: Read this.

Play and pause an MP3 file in C++

The C++ program to play and pause an audio file has been given here. Before executing the program you must:

  • Save your mp3 file in the same folder as the program or you’ll have to specify the path of the file while opening it using mciSendString() function.
  • type “-lwinmm” in the linker section of your compiler. In Dev C++, go to project-> project options-> parameters-> linker section and type “-lwinmm”.

In this program, we are going to use mciSendString() function with different arguments for the following operations:

open an MP3 file in your program

mciSendString("open \"Music.mp3\" type mpegvideo alias mp3", NULL, 0, NULL);

The first argument is a multimedia command string. This command string is used to open the audio file for further operations such as play, pause, set, etc. The alias for the file used in the program is mp3. You can choose your own name for it and used it for other command strings used with mciSendString() function.

Play an MP3 file in C++

mciSendString("play mp3", NULL, 0, NULL);

The command string “play mp3” is used to play the audio file provided. If your alias name is something else, replace it with mp3.

Pause an MP3 file

mciSendString("pause mp3", NULL, 0, NULL);

This is used to pause the audio file.

Close the MP3 file

mciSendString("close mp3", NULL, 0, NULL);

This command string closes the audio file and terminates all operations on the file.

The example program is as follows:

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


int main()
{
   int n;
   time_t t1, t2, previous_pause_time=0;
   
   //open the audio file
   mciSendString("open \"Music.mp3\" type mpegvideo alias mp3", NULL, 0, NULL);
   
   while(1)
   {
   	
   	cout << "Press 1 to play the file and press 2 to exit the file." << endl;
      cin>>n;
      if(n==1)
      {	
      	//play the audio file
   		t1=time(0);
   		mciSendString("play mp3", NULL, 0, NULL);
   		cout<<"Audio file playing...\n\n";
   	}
   	else if(n==2)
   	{
   		//close the file and get out of the loop
   		mciSendString("close mp3", NULL, 0, NULL);
   		break;
   	}

   	cout << "Press 0 to pause the file and press 2 to exit the file." << endl;
   	cin>>n;
   	if(n==0)
   	{
   		//pause the audio file
   		mciSendString("pause mp3", NULL, 0, NULL);
   		t2=time(0);
   		cout<<"Audio file paused after " << t2-t1+previous_pause_time << " seconds.\n\n";
   		previous_pause_time+=t2-t1;
   	}
   	else if(n==2)
   	{
   		//close the audio file
   		mciSendString("close mp3", NULL, 0, NULL);
   		break;
   	}
   		
   }
  

   return 0;
}

Output:

Press 1 to play the file and press 2 to exit the file.
1
Audio file playing...

Press 0 to pause the file and press 2 to exit the file.
0
Audio file paused after 6 seconds.

Press 1 to play the file and press 2 to exit the file.
1
Audio file playing...

Press 0 to pause the file and press 2 to exit the file.
0
Audio file paused after 14 seconds.

Press 1 to play the file and press 2 to exit the file.
1
Audio file playing...

Press 0 to pause the file and press 2 to exit the file.
0
Audio file paused after 17 seconds.

Press 1 to play the file and press 2 to exit the file.
2

As you can see in the program, we have used a while loop for continuous play and pause operations for the mp3 file. The seconds are the duration of the file after which it has been paused. Once the user chooses to exit, we close the audio file and break the loop.

Thank you.

Also, read: How to play a part of an MP3 file in C++

Leave a Reply

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