Playing sounds with C++? - c++

How would i make it so in my program there is a button when that button is clicked i want it to play a .wma file without opening and media player?

The C++ standard does not include this functionality. That means it depends on what your system offers.
For Windows, you can try something like PlaySound.
However, you best bet is to use a pre-existing library, like:
OpenAL
BASS
FMOD
SDL's: Sound.
Searching for C++ Sound Library brings up a lot of information.
Also, check out these three other SO topics:
What Is The Best C++ Sound API For Windows?
How to play MP3 files in C?
Learning to work with audio in C++

Although the above answer mentions it (and everything else, it doesn't give any recommendations so...), FMOD is the king of C++ sound (the most used) and works great so I'd recommend FMOD in particular.

Gstreamer is a free, cross-platform multimedia framework written in C (using GObject) that allows encoding/decoding for many types of media, including wma. Very easy to use and well documented.

KDE's Phonon is a cross-platform(!) C++ multimedia library. It supports native sound systems as backends. Worth a look if you haven't seen it before, and a simple video player can be written in a mere 83 lines of code.

For Windows, in all honesty your best bet is DirectShow. The RenderFile API allows you to play most audio file types with just a few lines of code.
The best part about DirectShow is that it's a part of the Windows platform so you don't need to bundle an external component.

Related

How to play a sound in C++?

I want to play a sound using C++. I'm not playing an audio file, but a certain voice. (Maybe functions like play_sound(frequency, amplitude, time).).
I want it to be cross-platform.
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

Simple C++ Sound API

My commercial embedded C++ Linux project requires playing wav files and tones at individual volume levels concurrently. A few examples of the sounds:
• “Click” sounds each time user presses screen played at a user-specified volume
• Warning sounds played at max-volume
• Warning tones requested by other applications at app-specified volume level (0-100%)
• Future support for MP3 player and/or video playback (with sound) at user-specified volume. All other sounds should continue while song/video is playing.
We're using Qt as our UI framework which has QtMultimedia and Phonon support. However, I heard the former has spotty sound support on Linux and the latter is an older version and may be deprecated in an upcoming Qt release.
I've done some research and here are a few APIs I've come across:
KDE Phonon
SFML
PortAudio
SDL_Mixer
OpenAL Soft
FMOD (though I'd prefer to avoid license fees)
ALSA (perhaps a bit too low-level...)
Other considerations:
Cross-platform isn't required but preferred. We'd like to limit dependencies as much as possible. There is no need for advanced features like 3D audio or special effects in the foreseeable future. My team doesn't have much audio experience so ease-of-use is important.
Are any of these overkill for my application? Which seems like the best fit?
Update:
It turns out we were already dependent on SDL for other reasons so we decided on SDL_Mixer. For other Embedded applications, however, I'd take a long at the PortAudio/libsndfile combo as well due to their minimal dependencies.
libao is simple, cross-platform, Xiphy goodness.
There's documentation too!
Usage is outlined here - simple usage goes like this:
Initialize (ao_initialize())
Call ao_open_live() or ao_open_file()
Play sound using ao_play()
Close device/file using ao_close() and then ao_shutdown() to clean up.
Go for PortAudio. For just plain audio without unneeded overhead such as complex streaming pipelines, or 3D, it is the best lib out there. In addition you have really nice cross-platform support. It is used by several professional audio programs and has really high quality.
i have used SDL_Mixer time and time again, lovely library, it should serve well for your needs, the license is flexible and its heavily documented. i have also experimented with SFML, while more modern and fairly documented, i find it a bit bulky and cumbersome to work with even tho both libraries are very similar. imo SDL_Mixer is the best.
however you might also want to check out this one i found a few weeks ago http://www.mpg123.de/, i haven't delved too much into it, but it is very lightweight and again the license is flexible.
There is a sound library called STK that would meet most of your requirements:
https://ccrma.stanford.edu/software/stk/faq.html
Don't forget about:
FFmpeg: is a complete, cross-platform solution to record, convert and stream audio and video.
GStreamer: is a library for constructing graphs of media-handling components. The applications it supports range from simple Ogg/Vorbis playback, audio/video streaming to complex audio (mixing) and video (non-linear editing) processing.

Playing Sound in C++

I need to play sound in C++.
It should be compatible across all platforms.
Thanks,
goerdy
Try OpenAL, FMOD or SDL_mixer. The choice is wide.
std::cout << "\a\a\a\a\a\a\a\a";
)))
My suggestion is to go with OpenAl (3D cross platform Audio API)
It's fairly easy to learn and meant to have a similar flow of programming as OpenGl. It's free and has different extensions to take advantage of sound cards. OpenAl has a library akin to OpenGl's Utility Toolkit (GLUT) that's called freealut. It makes things even simpler.
This might be a good place to get started:
http://www.devmaster.net/articles/openal/
Some of the functions in that code are deprecated but reading the documentation, you'll be able to update it.
The devmaster tutorial also teaches you how to load ogg vorbis (royalty free) files into memory and play them. You'll need to download and build libvorbis and libogg to do that but there's good documentation and, in case you're using Visual Studio, already made solutions.
The fact that Creative Labs (largest sound card manufacturer) is a major contributor to the project is a great point in it's favor. Hardware will not be ignored.
Last thing, remember to download the OpenAl SDK and link to the proper libraries and include directories. There's always a lot of people asking for specific files stored inside it.
"Compatible to all platforms" is a bit of a tall order. After all, "all platforms" includes the controller in your car, the regulator in a nuclear plant, and the router in your network.
If you mean "all desktop platforms", you may have a bit more luck. While it is impossible to produce sound in 100% standards C++, there are libraries that are available. However, I don't think that even these will get you "all platforms". Are you willing to settle for "most common platforms"?

if i want to play mp3's what is the difference between lame and MCI(VFW32)?

basic question , i need to play mp3's in my application in windows
when goggling i got allot of tutorials about VFW32.lib to play mp3's
and i know lame , what is the best option for playing mp3's in c++?
VfW is an API that predates DirectShow on Windows, and was the original API that provided access to the system's video codecs. I'd wager that today, it's extremely non-portable, and shouldn't be used. Instead, prefer either the DirectShow API or a separate library like either Lame or libmad or a framework like ffmpeg.

How can one play a FLV file from Youtube in a C++ application?

I'm using C++ to get Youtube video links, but now I would like to be able to play a stream in my application.
Using C++ wxWidgets, what cross platform options do I have?
Look at existing video players like mplayer or vlc that can both play flv files.
It you want a something a a somewhat lower level, Qt::media supports Gstream that supports flv. However this might go against what you want as you use wxwidgets.
Then, you can directly look into integrating GStreamer in your app.
The wxMediaCtrl class in wxWidgets is used to play video:
http://docs.wxwidgets.org/stable/wx_wxmediactrl.html
http://docs.wxwidgets.org/trunk/classwx_media_ctrl.html
On MacOS X and Windows wxMediaCtrl uses the native backend, and on Linux GStreamer. I don't know if the native backends on the first two support flv.
FFMPEG supports demuxing and decoding of the FLV format (among others), and is the best implementation I know of besides Flash itself. It's relatively easy to use, and it's used by all of the software others have suggested for decoding of Flash Video.
After some searching in google I believe there isn't something pre-made in C++ that will match WxWidgets but you should be able to find a decoder and use it's API.