How to play a sound in C++?
Ycao,
As far as I know, you will need some third party library.
Personally, I would use FMOD.
It's quite simple to use, well documented, you will find a lot code snippet to bootstart your project and it has a lot of interesting features.
FMOD Website
Is there a way to pause/stop a mp3 file playing with mcisendstring with the wait option?
You can use the wait/stop commands if the play command will be modified to avoid waiting until the *.mp3 has finished playing.
mciSendString("play mp3", NULL, 0, NULL);
Including winmm.lib does not work
If you read the compiler output carefully, you will see this warning:
warning: ignoring #pragma comment [-Wunknown-pragmas]
That means your compiler (GCC) does not support your code's use of #pragma comment
, and so winmm.lib
ends up not getting linked into your final executable, thus causing the subsequent linker error:
undefined reference to `_imp__mciSendStringA@16'
This is not a matter of the linker not being able to find winmm.lib
, it is a matter of the linker not being told to use winmm.lib
in the first place.
#pragma
is used to invoke compiler-specific commands. Not all compilers implement #pragma comment
. VC++ does (and so do a few others, like BCC32/64), but GCC does not. The other question you linked to was tagged visual-c++
, so #pragma comment
was appropriate in that case.
In your case, you will have to adjust your build process accordingly to use another way to tell the linker to use winmm.lib
. You do that in GCC by using the -l
option when invoking the linker, eg -lwinmm
.
C++/Win32 - What in my MCI utilization is causing a delay?
I was talking to a friend, and he recommended that I use a thread. "Well, duh", I thought. "How could I possibly have not thought of that?"
Related Topics
Cyclic Dependency Between Header Files
Run a Program with More Than One Source Files in Gnu C++ Compiler
A Class Name Introduced Inside a Class Is Not Treated as a Nested Class Name
C++ Using Class Method as a Function Pointer Type
Static Member Access in Constant Expressions
Convert a Single Color with Cvtcolor
How to Do the Equivalent of Memset(This, ...) Without Clobbering the Vtbl
Assertion Failed (Size.Width>0 && Size.Height>0)
How to Pause a Pthread Any Time I Want
Order of Constructor Call in Virtual Inheritance
Can Compiler Generate Std::Move for a Last Use of Lvalue Automatically
Is Std::String Size() a O(1) Operation
Why Callback Functions Needs to Be Static When Declared in Class
Why Is a Constexpr Function on a Reference Not Constexpr
C++: How to Implement Polymorphic Object Creator to Populate a Table