我正在为学校的一个游戏项目作为团队的一部分。我需要设置一个单独的类,为游戏提供音频。我们正在使用QT进行手机移动,并可能使用手机。我们决定使用声子作为游戏音频。我对此非常感兴趣,刚刚开始使用QT,也是第一次使用游戏编程。QT声子游戏音频
音频系统应该能够同时处理多个声音。至少它必须处理背景音乐和音效。效果将通过信号连接到插槽。
这里是我的代码:
/* ** * ** * ** * ****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();
}
....................... ....等
typical implementation:
AudioSystem::instance()->playMusic
AudioSystem::instance(), SLOT(click_sound())
因为我已经设置了似乎工作与简单的主窗口的简单情况确定的,但,当我把任何地方在我们的代码它什么都不做的代码。我错过了什么?
完整的项目: 混帐://gitorious.org/gamecs340project/gamecs340project.git
我发现它确实有效。 ...有点。操作不一致,但会一次播放两个声音。我在QtSDK/Demos/4.7/mobile/quickhit目录中找到了我想要做的一个很好的例子。他们使用GE(游戏引擎),它也具有我需要的声音缓冲功能。我希望我早点发现了这一点。 Game enabler似乎使用QT多媒体库。 – Venk