如果已经在播放,QT不播放声音?
如果另一个声音已经在播放,如何确保该声音不会开始播放 Im使用以下代码播放声音:如果已经在播放,QT不播放声音?,qt,Qt,如果另一个声音已经在播放,如何确保该声音不会开始播放 Im使用以下代码播放声音: void MainWindow::displayNotification(QString message) { // Play sound notification QSound *sound = new QSound("://2_1.wav", this); sound->play(); } 有什么想法吗?在主窗口中存储一个指向播放QSound的指针,然后检查它是否已完成该
void MainWindow::displayNotification(QString message)
{
// Play sound notification
QSound *sound = new QSound("://2_1.wav", this);
sound->play();
}
有什么想法吗?在主窗口中存储一个指向播放QSound的指针,然后检查它是否已完成该功能
void MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
QString audiofile("://2_1.wav");
m_pSound = new QSound(audiofile, this);
if(!m_pSound)
{
qDebug() << "Failed to initialise sound file: " << audiofile;
}
}
void MainWindow::displayNotification(QString message)
{
if(!m_pSound) // check m_pSound is initialised
return;
// check if sound is playing
if(!m_pSound->isFinished)
return;
// Play sound notification
m_pSound->play();
}
void MainWindow::MainWindow(QWidget*父项)
:QMainWindow(父级)
{
QString音频文件(“://2_1.wav”);
m_pSound=新的QSound(音频文件,本文件);
如果(!m_pSound)
{
qDebug()播放();
}
请注意,m_pSound现在声明为MainWindow的成员变量。因此类“QSound”具有方法“isFinished()”
if(m_pSound->isFinished())
//play
else
//wait
如果(!m_pSound->isFinished)我在my.h中这样声明它:
QSound*m_pSound;
有什么想法吗?你需要初始化m_pSound ptr,然后在取消引用指针之前检查它是否有效;请在我的答案中查看代码中的更改。谢谢!现在工作得很好:)