Reputation: 21
Im new in QT , i want to play a music file through QT ,with the interface containing a single play button ,so that when i click the play button the song should play.now when i run the program,i get my interface but unfortunately when i click the play button,it says the .exe file stopped working ,and it gets closed down ,with an exit error code of 255 getiing dispalyed in QT creator window..here is the main window.cpp file
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "audiere.h"
using namespace audiere;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//connect(ui->Number1,SIGNAL(textChanged(QString)),this,SLOT(numberChanged()));
connect(ui->play,SIGNAL(clicked()),this,SLOT(PLAY()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::PLAY() {
AudioDevicePtr device(OpenDevice());
OutputStreamPtr sound(OpenSound(device,"lk.mp3",true));
sound->play();
sound->setRepeat(true);
sound->setVolume(2.0);
}
Upvotes: 2
Views: 177
Reputation: 19122
I have three recommendations:
First, add in error checking.
Second, consider using Ogg Vorbis, if you are having problems with mp3's.
Third, move your pointers to be member variables of MainWindow instead of local scope variables. Audiere is probably cleaning them up prematurely.
This is from "tutorial.txt" that ships in the doc folder of the Audiere download:
You need to open an AudioDevice before you can play sounds...
AudioDevicePtr device(OpenDevice());
if (!device) {
// failure
}
Now that we have a device, we can actually open and play sounds.
/*
* If OpenSound is called with the last parameter = false, then
* Audiere tries to load the sound into memory. If it can't do
* that, it will just stream it.
*/
OutputStreamPtr sound(OpenSound(device, "effect.wav", false));
if (!sound) {
// failure
}
/*
* Since this file is background music, we don't need to load the
* whole thing into memory.
*/
OutputStreamPtr stream(OpenSound(device, "music.ogg", true));
if (!stream) {
// failure
}
Great, we have some opened streams! What do we do with them?
There also is a caveat in the faq page:
As of the 1.9.2 release, Audiere supports MP3 thanks to the splay library. However, there is very little LGPL-compatible MP3 code out there that works across a wide range of MP3s and hardware. I highly recommend using Ogg Vorbis for all of your music needs. It uses less CPU time by about a factor of five and sounds better.
And at the bottom of the tutorial it mentions when clean up happens:
When you are done using Audiere, just let the RefPtr objects go out of scope, and they will automatically clean themselves up. If you really must delete an object before its pointer goes out of scope, just set the pointer to 0.
Hope that helps.
Upvotes: 0