This is a mini project with C++ to create a command line based music player using mciSendStrings
Sample Screenshots
Introduction:
The Project to be developed is a Command Line Based Music Player completely written in C++. It should play, pause and shuffle all music files in a specific directory and all it’s interface should be in C++ command line terminal. We have divided the code into three files namely files.h , define.h , and main.cpp so as to improve code readability and debugging.
Objective:
The main objective is to play any non variable bit rate mp3 file using mciSendStrings() function defined in C++ windows.h header file.
Instructions
-
To run this code in visual studio code you have to add linker library -lwinmm here It's a Windows Multimedia Library
-
If you are using code blocks or Visual Studio IDE you can directly add path of lwinmm in linker library options.
-
If you want to add more songs, place constant bitrate mp3 files in the 'songs' folder which is in the same directory as the main.cpp file (up to 9 mp3s are supported). Song filename must follow this convention : (artistname)-(songname)-(duration).mp3 without any whitespace. Example - OzzyOsbourne-Dreamer-4.44.mp3 Note - Space should not be used in filenames.
Problem Faced :
- mciSendStrings documentation by Microsoft was very vague and lacked crucial informations like examples to demonstrate the working of its function.
- mciSendStrings has 2 versions for its string command parameters. One accepts ANSI encoded characters while other accepts UNICODE and there was no proper information available regarding this.
- To use mciSendStrings we need to link a library called -lwinmm (Windows Multimedia Lib) but there was no direct way to implement this Visual Studio Code as all the development work is done of Visual Studio IDE.
- Windows Media Library provides very limited options in regards to the attributes of media file such as it could only play mono bit rate audio files with greatly reduced sample rate and hence offering poor sound quality.
We have also implemented error handling so that is any audio file could not be played for any reason the program will automatically skip to the next music in the list.
Conclusion :
There are many overheads to make a fully fledged music player via C++, but with the help of above code we have demonstrated a good example of how to make this work on C++ despite of having so many compatibility issues.
Working Environment:
- Visual Studio Code - 1.56.2
- C++ standard 17
- Operating System – Windows 10 64 Bit
- C++ Compiler – MinGW gcc x64
References
- CodeProject - https://www.codeproject.com/Articles/14709/Playing-MP3s-using-MCI
- Stackoverflow - https://stackoverflow.com/questions/22253074/how-to-play-or-open-mp3-or-wav-sound-file-in-c-program
- Codespeedy - https://www.codespeedy.com/play-a-part-of-an-mp3-file-in-cpp/
- Github - microsoft/vscode-cpptools#6191