Reputation:
In my resource files I have 31 samples of engine sounds. I would like to make a slider, which moving will change a sample for another one(going higher engine starts to yell, lower it is going calmer). My code from slider:
void MainWindow::on_poziomMocy_sliderMoved(int position)
{
QMediaPlayer * music = new QMediaPlayer();
//play umieścić w caseach i kończyć je stop. Tak jak to miałem wcześniej, dodać cały plik
//do resources
switch(position)
{
case 0:
{
music->stop();
music->setMedia(QUrl("qrc:/sounds/dźwięki/859.wav"));
music->play();
}
case 1:
{
music->stop();
music->setMedia(QUrl("qrc:/sounds/dźwięki/919.wav"));
music->play();
}
case 2:
{
music->stop();
music->setMedia(QUrl("qrc:/sounds/dźwięki/984.wav"));
music->play();
}
case 3:
{
music->stop();
music->setMedia(QUrl("qrc:/sounds/dźwięki/1052.wav"));
music->play();
}
case 4:
{
music->stop();
music->setMedia(QUrl("qrc:/sounds/dźwięki/1126.wav"));
music->play();
}
}
When I play this and slide a slider samples are overlaping and my speakers are crying. I don't know what to do the "music" scope is right, why does stop() do not work properly?
Upvotes: 0
Views: 154
Reputation: 4010
This is because of each time you slide a slider new QMediaPlayer
created:
QMediaPlayer * music = new QMediaPlayer();
You should place player creation at some another place. For exaple at your class constructor.
Upvotes: 1