Reputation: 1421
I want to play a sound on buttonpress, I have a working code for that.
Phonon::MediaObject *bulletPlay = new Phonon::MediaObject(this);
bulletPlay -> setCurrentSource(Phonon::MediaSource("newBullet3.wav"));
playOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
Phonon::createPath(bulletPlay, playOutput);
bulletPlay->play();
But doing all of these things on button press causing performance hurt. I understand that every-time disc access is the reason. (Well.. you can say that file will anyway be in the ram and therefore there is no disk access on every button press.. buffer cache and all.. but THIS code is causing performance hurt badly.)
So what I simply did was
static Phonon::MediaObject *bulletPlay;
static Phonon::AudioOutput *playOutput;
static Phonon::MediaSource *filePath;
in my class. And in the constructor
bulletPlay = new Phonon::MediaObject(this);
filePath = new Phonon::MediaSource("newBullet3.wav");
bulletPlay -> setCurrentSource(*filePath);
playOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
Phonon::createPath(bulletPlay, playOutput);
and bulletPlay->play();
on buttonpress.
But it plays sound only the first time. So it seems I have to set current resource every time using bulletPlay -> setCurrentSource(*filePath);
. So How do I go about it? I do not want to set current resource every time.. I can not test application for performance test also since testers are on client side..
Upvotes: 0
Views: 1263
Reputation: 22272
You'll need to create a separate slot
in your class that the button triggers instead of connecting directly to the buttonPlay->play()
button. The problem that you're seeing is that the audio buffer that the buttonPlay
object is using is "at the end". So you need to tell it to start again from the beginning, which is what bulletPlay -> setCurrentSource(*filePath);
is doing for you. You don't need to create a new resource, you should be able to tell the existing resource to reuse the current one (but just start from the beginning).
Upvotes: 1