QMediaPlayer:发出positionChanged()时声音中断

QMediaPlayer: Sound interrupts when positionChanged() is emitted

本文关键字:声音 中断 发出 positionChanged QMediaPlayer      更新时间:2023-10-16

我也有类似QMediaPlayer positionChanged()的问题。滑块更新时声音爆发

我使用QMediayPlayer,每次发出positionChanged()信号来更新滑块位置,并为滑块设置一个新值时,声音都会中断一段时间。

这是在构造函数中:

soundfile = new QMediaPlayer(this, QMediaPlayer::LowLatency); //soundfile is a pointer of a QMediaPlayer Object
QObject::connect(soundfile, SIGNAL(positionChanged(qint64)), this, SLOT(changedPosition(qint64)));

这是插槽功能:

void Soundfile::changedPosition(qint64 p) {
    QTime time(0,0,0,0);
    time = time.addMSecs(soundfile->position());
    if(p != 0) recordSlider->setValue(p); //THIS IS THE LINE, WHERE IT INTERRUPTS
    changeRecordTime(QString::number(p));
    recordPositionLabel->setText("Aktuelle Zeit: " + time.toString());
}

recordSlider是一个QSlider。如果我用setValue注释掉这行,一切都很好。

有人有主意吗?

我认为问题是:当媒体播放器发出SIGNAL时,会调用SLOT,当您在函数中使用setValue时,setValue会再次发出SIGNAL,并且该过程会再次发生。

为了解决这个问题,我禁用了滑块跟踪,并使用setSliderPosition移动位置。

示例:

slider->setTracking(false);
slider->setSliderPosition(pos);