7

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.

Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589

6 Answers6

9

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
Xantium
  • 11,201
  • 10
  • 62
  • 89
Northwood
  • 101
  • 1
  • 3
8

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);
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
  • A few potential gotchas with this snippet: 1. Make sure that the path to your file is correctly quoted \"\" 2. I found that sending "close song1" immediately after "play song1" had the effect of the sound not playing. i.e. The sound would be opened, then immediately closed. – Dave Apr 01 '14 at 16:42
3

IGraphBuilder::RenderFile is an easy way to play any audio file.

MSalters
  • 173,980
  • 10
  • 155
  • 350
Goz
  • 61,365
  • 24
  • 124
  • 204
2

Youc could use MCI windows functions, https://msdn.microsoft.com/en-us/library/ms709626

It can play many of audio file formats including MP3, WAV, MIDI etc.

If I recall correctly it does not require DirectX.

The PlaySound function might also work for you.

PhilLab
  • 4,777
  • 1
  • 25
  • 77
Jason
  • 2,341
  • 17
  • 14
0

If you don't want to pay any licence and wanna do in-house, do the parsing of your mp3 file and pass it to XAudio2. Its a thing that you can do once (2-3 hours at max) and use always. :P

feal87
  • 927
  • 3
  • 11
  • 29
  • How is that better than DirectShow? Both are MS APIs, but users will already have DShow (probably). Is there an advantage? – Mr. Boy Jan 12 '10 at 15:56
  • DirectShow have another target, is for "multimedia" as for multimedia the sense is very broad. (and is anyway now not anymore updated) XAudio2 is the replacement to DirectSound, is a low-level Audio API cross-platform Xbox360/PC. Microsoft want to push it as the only API to be usen on Windows. – feal87 Jan 12 '10 at 20:05
-1

You could have a look at BASS. It's a simple to use audio library, free for noncommercial use.

Thomas
  • 174,939
  • 50
  • 355
  • 478