Hi all,
I have made a test project to play sound with Qt.
I use SFML 2.0 Windows 32 bits - GCC SJLJ (Code::Blocks)
the Qt project:
#################################
QT += core gui
#QT += phonon
CONFIG += debug
TARGET = TestLoadSound
TEMPLATE = app
#DEFINES += SFML_STATIC
LIBS += -lsfml-audio
LIBS += -L"D:/SFML-2.0-rc/lib"
INCLUDEPATH += "D:/SFML-2.0-rc/include"
SOURCES += main.cpp\
window.cpp
HEADERS += window.h
################################
window.h
###################################
#ifndef WINDOW_H
#define WINDOW_H
#include <QtGui/QWidget>
//#include <Phonon/AudioOutput>
//#include <Phonon/MediaObject>
#include <SFML/Audio.hpp>
class Window : public QWidget
{
Q_OBJECT
public:
Window(QWidget *parent = 0);
~Window();
public slots:
void LoadAndPlaySound();
private:
//Phonon::MediaObject *player;
//Phonon::MediaObject *player2;
//Phonon::AudioOutput *audioOutput;
sf::SoundBuffer buffer;
sf::Sound sound;
std::string path;
};
#endif // WINDOW_H
##############################
window.cpp
###################################
#include "window.h"
#include <QtGui>
//#include <phonon>
Window::Window(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
QPushButton *btn = new QPushButton(tr("Play Sound"));
connect(btn,SIGNAL(clicked()),this,SLOT(LoadAndPlaySound()));
layout->addWidget(btn);
setLayout(layout);
/*
player = new Phonon::MediaObject(this);
player2 = new Phonon::MediaObject(this);
audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory,this);
Phonon::createPath(player,audioOutput);
Phonon::createPath(player2,audioOutput);
*/
//buffer = new sf::SoundBuffer();
//sound = new sf::Sound();
QString file = QApplication::applicationDirPath() + QDir::separator() + "117-Fire01.wav";
path = file.toStdString();
}
Window::~Window()
{
}
void Window::LoadAndPlaySound()
{
/*
//QString file = QCoreApplication::applicationDirPath() + "/117-Fire01.wav";
QString file = QCoreApplication::applicationDirPath() + "/117-Fire01.wav";
QString file2 = QCoreApplication::applicationDirPath() + "/120-Ice01.wav";
//QString file = QCoreApplication::applicationDirPath() + "/hello goodbye & hello - anri kumaki.mp3";
if(QFile::exists(file)){
//qDebug() << file + " exists.";
//Phonon::MediaSource source(file);
player->setCurrentSource(file);
//Phonon::createPath(player,audioOutput);
player->play();
player2->setCurrentSource(file2);
player2->play();
}else{
qDebug() << file + " does not exist.";
}
//QSound::play("117-Fire01.wav");
//Phonon::MediaObject *music = Phonon::createPlayer(Phonon::MusicCategory,Phonon::MediaSource("Fire.ogg"));
//music->play();
*/
bool ok = buffer.loadFromFile(path);
if(ok)
{
sound.setBuffer(buffer);
sound.play();
}else
{
qDebug() << "Failed";
}
}
########################################
main.cpp
########################################
#include <QtGui/QApplication>
#include "window.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}
#######################################
the program can run and play the sound, but when close the program, it crashed.
Searched forum, it is a known issue of openal dynamic lib?
Could this problem solved by static linking sfml-audio ?
but how to static link?
I tried add
defines += SFML_STATIC
and
LIBS += -lsfml-audio-s
but fail to complie...
Could any one help?
Thanks