Simplest way to play mp3 from Visual C++

五迷三道 提交于 2019-11-29 15:11:41

问题


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.


回答1:


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);



回答2:


This is an easy way to play any audio file: http://msdn.microsoft.com/en-us/library/dd390090(VS.85).aspx




回答3:


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.




回答4:


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



回答5:


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




回答6:


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



来源:https://stackoverflow.com/questions/2049825/simplest-way-to-play-mp3-from-visual-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!