Ingo Siegert
Ingo Siegert

Reputation: 31

QMediaPlayer crashes when pausing playback

I have a longer wav-file, where I wanted to play smaller parts. I stored startTime and endTime as qint64 and already loaded the audiofile:

player = new QMediaPlayer;
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(slotTick(qint64)));

player->setNotifyInterval(10);
player->setMedia(QUrl::fromLocalFile(mediasource));
...
player->setPosition(startTime);
player->play();

I observe the position with the positionChanged Signal andd use the following slot, to stop the playback once the end of the desired part is reached:

void PlayerWidget::slotTick(qint64 time){
if(endTime >= 0 && time >= endTime){
    if(player->state() == QMediaPlayer::PlayingState){
        player->stop();
    }
}

Unfortunately, the program crashes shortly after the player stops. WHat could be the reason

Upvotes: 3

Views: 1053

Answers (1)

Natsukane
Natsukane

Reputation: 681

Just had the same issue, and while I don't really know the underlying reason, I managed to somehow intuit a solution that actually works.

Instead of calling player->stop() directly, trigger it with a singleShot QTimer. Python code equivalent: QtCore.QTimer.singleShot(0, player.stop).

Upvotes: 1

Related Questions