PDA

View Full Version : Phonon stops playing after a while



alenn.masic
5th September 2012, 16:30
Hi all

I'm having problems with Phonon. I've written application which plays some sound on KeyPressed event. But after a minute or less the volume of that sound is reduced and then it completly stops, sometimes it just stops. I've never used Phonon before so I think that my code is wrong. Can you help me out with this.


bool MainWindow::eventFilter(QObject *o, QEvent *e)
{
if(e->type() == QEvent::KeyPress)
{
music->play();
music->seek(0);
}
return ui->textEdit->eventFilter(o, e);
}

alenn.masic
6th September 2012, 07:48
I need to add that this sound is really short (less then second) and it's repeating al over every time user press keyboard button. Everything is ok, but then suddenly it just stop playing, and when it occurs I get different sound in my headphones, it sounds like when you unplug headphones from your PC, something like that.

d_stranz
6th September 2012, 15:52
bool MainWindow::eventFilter(QObject *o, QEvent *e)
{
if(e->type() == QEvent::KeyPress)
{
music->play();
music->seek(0);
}
return ui->textEdit->eventFilter(o, e);
}


I think the problem is that you are preventing the MainWindow from handling events as it should. Look at the example in QObject::eventFilter(). You want something like that:



bool MainWindow::eventFilter(QObject *o, QEvent *e)
{
if(e->type() == QEvent::KeyPress)
{
music->play();
music->seek(0);
}
return QMainWindow::eventFilter( 0, e );
}


Playing a note every time the user presses a key anywhere in your app might get pretty annoying after a while, don't you think? You sure you don't want to restrict this to specific widgets within your app (like the text edit, for example)?

alenn.masic
6th September 2012, 20:01
That was my first aproach, but with same results, so I tried with this return ui->textEdit->eventFilter(o, e); and with that sound lasts longer, but still it's not enough. So I made silly but effective solution, it's not very good but it works better then any of previous solutions:


int count = 0;
bool MainWindow::eventFilter(QObject *o, QEvent *e)
{
if(e->type() == QEvent::KeyPress)
{
music->play();
music->seek(0);
count++;
if(count == 30){
music->stop();
count = 0;
}
}
return ui->textEdit->eventFilter(o, e);
}