QT Phonon 游戏音频

3

我是一个团队的一员,正在为学校的游戏项目工作。我需要设置一个单例类来处理游戏音频。我们使用QT实现移动端的可移植性,可能还包括安卓手机。我们决定使用phonon来管理游戏音频。我对此非常陌生,第一次使用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();
}

........................... etc

typical implementation:
AudioSystem::instance()->playMusic  
AudioSystem::instance(), SLOT(click_sound())

我已经设置好的代码在一个简单的mainwindow中看起来工作正常,但是当我放到我们代码中的任何位置时它就无法工作。这里有什么我遗漏的吗?
完整项目:git://gitorious.org/gamecs340project/gamecs340project.git

我发现它实际上是可以工作的。有点不稳定,但可以同时播放两个声音。我在QtSDK/Demos/4.7/mobile/quickhit目录中找到了一个很好的例子,他们使用了GE(游戏启用器)。它还具有我需要的声音缓冲功能。我希望早些时候就能找到这个。游戏启用器似乎使用QT多媒体库。 - Venk
1个回答

0

Phonon无法做到这一点。至少不是以合理的方式;如果您尝试播放多个声音,它会尝试多次打开系统的音频设备。这并不能保证在任何地方都能正常工作。

对于游戏,通常需要自己的音频混合。其中最简单的方法之一是使用SDLSDL_mixer。SDL_mixer可以同时播放多个音频样本,但不能同时播放多个音乐流。如果您需要这样做,可以使用我修改过的SDL_mixer版本。

当然,还有许多其他库可供选择。但SDL_mixer可能是最容易使用的一个。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接