|
Location: Desktop development - C/C++ License: The Microsoft Public License (Ms-PL) Simple C++ DirectShow MP3 Player ClassPosted by Wong Shao VoonA simple C++ MP3 player class based on DirectShow |
Skill: BeginnerPosted: 19/07/2011Views: 653Rating: 5.00 /5Popularity: 0.00 |
| Sign Up to vote for this article |

If you need to just play MP3s in your application (for example, play a short MP3 during the application splash screen), Mp3 class is a no frills C++ MP3/WMA DirectShow player class, for such simple needs. The original code is from flipcode's contributor, Alan Kemp. The original code need a bit of tweaking to include the necessary header files and import libraries to get it to compile in Visual Studio 2010. Since this class relies on DirectShow, you need to download the Windows SDK to build it. If you are using Visual Studio 2010, it actually comes with a subset of the Windows SDK which includes the DirectShow lbraries, so you can build this class without downloading anything. You have to call COM's CoInitialize to initialize COM's runtime before calling the Load on mp3 file. And you have to also call CoUninitialize at the end of your application, after the Cleanup is called. The header file, Mp3.h is listed below.
The original class only has the play, pause and stop functionality. Note: after calling Pause, you have to call Play to resume playing. Since I have a need to loop my music, so I need to know when my MP3 ended, I added the method, WaitForCompletion for me to poll periodically whether the playing has ended, to replay it again. Since the original code always played at full volume, I have also added a method, GetVolume to get volume and another method, SetVolume to set volume. Note: -10000 is the minimum volume and 0 is the maximum volume. And if you set any positive volume greater than 0, you will receive an error. You can call GetDuration and GetCurrentPosition to get the duration of the MP3 and the current playing (time) position of the MP3 respectively. These 2 methods return units of 10th millionth of a second(1/10,000,000 of a second): you have to divide by 10,000,000 to get the duration in seconds. The reason I did not return the duration in seconds, is because I found that second unit is too coarse grained to do seeking when seeking is to be implemented in the future. Right now, seeking is not supported but it is planned to be implemented in near future. The source code implementation of Mp3.cpp is listed below.
The source code includes a static library project and the DLL project and a demo project, PlayMp3, which plays MP3 with a helper class, CLibMP3DLL, to load the LibMP3DLL.dll at runtime. Usage of CLibMP3DLL is similar to Mp3 class, with additional LoadDLL and UnloadDLL methods to load/unload dll. Below is the header file of CLibMP3DLL.
Though I may have added a few methods to the Mp3 class, it took me quite a bit effort to get them run correctly. I hope to pass these time-savings to other developers who simply wants to play a MP3 file, minus the hassle. This class is hosted at Codeplex.
This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)
| Wong Shao Voon
| I guess I'll write here what I does in my free time, than to write an accolade of skills which I currently possess. I believe the things I does in my free time, say more about me. When I am not working, I like to watch Japanese anime. I am also writing some movie script, hoping to see my own movie on the big screen one day. I like to jog because it makes me feel good, having done something meaningful in the morning before the day starts. I also writes articles for IntelliProject; I have a few ideas to write about but never get around writing because of hectic schedule. Location: |
Sign up to post message on the article message board!