Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to play mp3 from Visual C++

A few years back, I wrote some util library around DShow/DSound to let me play MP3s in a Windows C++ application. Is that still the normal way to do it in a C++/MFC app, or is that an area of DirectX that has been subsumed into the general Windows APIs?

The motivation is simply we use the standard Windows PlaySound method for WAVs, and would like to be able to play MP3s using a similarly simple API, either provided by Windows or something we write to wrap more complex functionality.

EDIT: this is for a large, commercial, closed-source project. And we only want to play things simply, paying a lot for a library won't fly.

like image 963
Mr. Boy Avatar asked Jan 12 '10 14:01

Mr. Boy


People also ask

How do I read an MP3 file?

In Windows 10, MP3s are played by default in the Windows Media Player; in macOS, they're played in iTunes. All you have to do is double-click on the MP3 file you want to listen to and by default, your audio player will open the file and start playing.


2 Answers

You can either use DirectShow but it's not part of DirectX anymore or rely on a third-party library like Bass, FMod, mpg123 or even libwmp3.

If you don't want to use DirectShow anymore (but why change if your existing code keeps working?), you can use MCI:

mciSendString("open la_chenille.mp3 type mpegvideo alias song1", NULL, 0, 0); 
mciSendString("play song1", NULL, 0, 0);
mciSendString("close song1", NULL, 0, 0);
like image 196
Gregory Pakosz Avatar answered Oct 19 '22 03:10

Gregory Pakosz


PlaySound() natively supports MP3 as long as it is embedded in a WAV file. People don't realize that WAV is a container format.

Download the ffmpeg utilities to convert the header and preserve the codec:

ffmpeg -i input.mp3 -c copy -f wav embedded_mp3.wav
like image 33
Northwood Avatar answered Oct 19 '22 03:10

Northwood