I am working on a game project for school as part of a team. I need to setup a singleton class that does audio for the game. We are using QT for portability to mobile and possibly to andriod phone. We decided to use phonon for the game audio. I'm very new at this and just started using QT for first time and also new to game programming.
The audio system should be able to handle more than one sound at one time. At least it must handle the background music and sound effects. The effects will be connected via Signals to slots.
Here is my code:
/ * ** * ** * ** * **** audiosystem.h * ** /
class AudioSystem : public QWidget
{
Q_OBJECT
public:
static AudioSystem *instance();
void setMusicFile(const QString &filename);
signals:
bool finishedMusic(); ///< For looping
public slots:
void playMusic(); ///< BG music triggered at Level start?
void stopMusic(); ///< Triggered by level finish
void click_sound(); ///< Menu button clicks
void step_sound(); ///< Other character sounds
void wall_sound(); ///< Hitting the wall or collision sound
void jump_sound(); ///< Jumping sound
void sound(int); ///< Level specific custom sounds
private:
// Singleton - constructors made private
AudioSystem(QWidget *parent = 0);
~AudioSystem();
AudioSystem(const AudioSystem &);
AudioSystem& operator=(const AudioSystem &);
static AudioSystem *m_Instance;
// media objects
Phonon::MediaObject *m_BgPlayer;
Phonon::MediaObject *m_EffectPlayer;
// audio sinks
Phonon::AudioOutput *m_BgAudioOutput;
Phonon::AudioOutput *m_EffectAudioOutput;
// audio paths
Phonon::Path m_BgAudioPath, m_EffAudioPath;
};
/ * ** * ** audiosystem.cpp **/
AudioSystem* AudioSystem::m_Instance = 0;
AudioSystem* AudioSystem::instance()
{
if (!m_Instance)
{
m_Instance = new AudioSystem();
}
return m_Instance;
}
AudioSystem::AudioSystem(QWidget *parent) :
QWidget(parent)
{
// create new instance of player and audio sinks then connect with paths
m_BgPlayer = new Phonon::MediaObject(this);
m_EffectPlayer = new Phonon::MediaObject(this);
m_BgAudioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
m_EffectAudioOutput= new Phonon::AudioOutput(Phonon::MusicCategory, this);
m_BgAudioPath = Phonon::createPath(m_BgPlayer, m_BgAudioOutput);
m_EffAudioPath = Phonon::createPath(m_EffectPlayer, m_EffectAudioOutput);
}
void AudioSystem::setMusicFile(const QString &filename)
{
m_BgPlayer->setCurrentSource(QString(filename));
}
void AudioSystem::playMusic()
{
m_BgPlayer->play();
}
void AudioSystem::stopMusic()
{
m_BgPlayer->stop();
}
void AudioSystem::click_sound()
{
m_EffectPlayer->setCurrentSource(QString(":/button.wav"));
m_EffectPlayer->play();
}
........................... etc
typical implementation:
AudioSystem::instance()->playMusic
AudioSystem::instance(), SLOT(click_sound())
The code as I have set up seems to work OK in a simple situation with a simple mainwindow but when I put any where in our code it does nothing. Is theresomething I am missing?
full project: git://gitorious.org/gamecs340project/gamecs340project.git
Phonon cannot do this. At least not in a sane manner; it tries to open the system's audio device multiple times if you try to play multiple sounds. This is not guaranteed to work everywhere.
For games, you usually want your own audio mixing. One of the easiest ways to do this is using SDL and SDL_mixer . SDL_mixer can play multiple audio samples at the same time, but not multiple music streams. If you want that, you can use my modified version of SDL_mixer .
There are many other libraries too for this, of course. But SDL_mixer is probably the easiest one to use.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.