View Full Version : playing sounds in c++
iron_guitarist1987
July 1st, 2010, 03:46 PM
What is the easiest way to play a sound file using c++?
iron_guitarist1987
July 1st, 2010, 06:23 PM
What is the easiest way to play a sound file in C++. I don't care what method is used, just an easy way to do it.
dwhitney67
July 1st, 2010, 10:04 PM
I forgot who it was on this forum, but one of the generous members posted this code before...
//
// Requires the use of libSDL_mixer; available from libsdl devel packages
//
// sudo apt-get install libsdl-mixer1.2-dev libsdl1.2-dev
//
// To build: g++ `sdl-config --cflags --libs` PlayMusic.cpp -lSDL_mixer -o play
//
// To run: play <file.mp3>
//
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <iostream>
int main(int argc, char** argv)
{
// Init
if (SDL_Init(SDL_INIT_AUDIO) != 0)
{
std::cerr << "SDL_Init ERROR: " << SDL_GetError() << std::endl;
return -1;
}
// Open Audio device
if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) != 0)
{
std::cerr << "Mix_OpenAudio ERROR: " << Mix_GetError() << std::endl;
return -1;
}
// Set Volume
Mix_VolumeMusic(100);
// Open Audio File
Mix_Music* music = Mix_LoadMUS(argv[1]);
if (music)
{
// Start Playback
if (Mix_PlayMusic(music, 1) == 0)
{
unsigned int startTime = SDL_GetTicks();
// Wait
while (Mix_PlayingMusic())
{
SDL_Delay(1000);
std::cout << "Time: " << (SDL_GetTicks() - startTime) / 1000 << std::endl;
}
}
else
{
std::cerr << "Mix_PlayMusic ERROR: " << Mix_GetError() << std::endl;
}
// Free File
Mix_FreeMusic(music);
music = 0;
}
else
{
std::cerr << "Mix_LoadMuS ERROR: " << Mix_GetError() << std::endl;
}
// End
Mix_CloseAudio();
}
Read the header of the file for instructions on how to build/run the app, which btw only plays MP3 files.
kknd
July 1st, 2010, 10:17 PM
There are many libraries for audio;
GStreamer ( http://www.gstreamer.net/ ) is a high level library that will probably suit your needs. It's not so easy to learn, but it's worth it.
Other libraries: Phonon (good if you're using Qt4) and audiere ( http://audiere.sourceforge.net/ ) .
audiere is the easiest, but it's more limited than GStreamer.
dwhitney67
July 1st, 2010, 10:19 PM
.
sushiboxdev
July 2nd, 2010, 04:15 AM
I would check out CLAM, its a bit more sophisticated than SDL_Mixer, if thats what your going for that is.
http://clam-project.org/
BenAshton24
July 2nd, 2010, 04:36 AM
I don't care what method is used, just an easy way to do it.
In that case, how about this:
system("mplayer sound.ogg");
It's cheating but if you want something simple, it works.
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.