当second
chages。时,我正试图播放声音。
在我的stopwatch
中有milliseconds
,它会在miliseconds'
更改下播放,而不是 seconds'
。我应该为*seconds*
创建一个条件。
但是不能正确地做到,你可以建议这个条件吗?
以下是我的方法(保留QMediaPlayer
milliseconds
的对象
):
我的构造函数和SLOTS:
StopMainWindow::StopMainWindow(QWidget *parent) :
QMainWindow(parent),
mRunning(false)
, mStartTime()
, mTotalTime(0)
{
//ON THE TOP THERE ARE QPUSHBUTTONS AND QLABELS OBJECTS
connect(pushButton_Start, SIGNAL(clicked()), SLOT(start()));
connect(pushButton_go_on, SIGNAL(clicked()), SLOT(pause()));
connect(pushButton_Stop, SIGNAL(clicked()), SLOT(stop()));
connect(pushButton_Close, SIGNAL(clicked()), SLOT(close()));
connect(pushButton_Back,SIGNAL(clicked()),SLOT(back()));
///////////////////////////////////////////////////////////////////////////////
setCentralWidget(centralWidget);
///////////////////////////////////////////////////////////////////////////////////////////////
pushButton_Start->setEnabled(true);
pushButton_go_on->setEnabled(false);
pushButton_Stop->setEnabled(false);
startTimer(0);
}
StopMainWindow::~StopMainWindow()
{}
void StopMainWindow::start(void)
{
pushButton_Start->setEnabled(false);
pushButton_go_on->setEnabled(true);
pushButton_Stop->setEnabled(true);
mStartTime = QDateTime::currentDateTime();
mRunning = true;
QMediaPlayer *music=new QMediaPlayer();
music->setMedia(QUrl("qrc:/sounds/tik.mp3"));
music->play();
}
void StopMainWindow::stop(void)
{
pushButton_Start->setEnabled(true);
pushButton_go_on->setEnabled(true);
pushButton_Stop->setEnabled(false);
mTotalTime = 0;
mRunning = false;
}
void StopMainWindow::pause(void)
{
pushButton_Start->setEnabled(true);
pushButton_go_on->setEnabled(false);
pushButton_Stop->setEnabled(true);
timerEvent(new QTimerEvent(0));
mTotalTime += mSessionTime;
mRunning = false;
}
void StopMainWindow::timerEvent(QTimerEvent *)
{
if(mRunning)
{
mSessionTime = mStartTime.msecsTo(QDateTime::currentDateTime());
qint64 time = mTotalTime + mSessionTime;
time *= 111;
unsigned int h = time / 1000 / 60 / 60;
unsigned int m = (time / 1000 / 60) - (h * 60);
unsigned int s = (time / 1000) - ((m + (h * 60))* 60);
unsigned int ms = time - (s + ((m + (h * 60))* 60)) * 1000;
const QString diff = QString("%1:%2:%3,%4").arg(h, 2, 10, QChar('0')).arg(m, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0')).arg(ms, 3, 10, QChar('0'));
mLabel->setText(diff);
}
}
答案 0 :(得分:0)
我建议只初始化QMediaPlayer
一次并通过QMediaPlaylist
添加声音以使QMediaPlaylist::Loop
连续运行,因为声音是准确的秒,不需要使用计时器。如您所希望QLabel
每毫秒更改一次,我们会使用startTimer(1)
。 complete example
StopMainWindow::StopMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::StopMainWindow)
{
[...]
connect(pushButton_Start, &QPushButton::clicked, this, &StopMainWindow::start);
connect(pushButton_go_on, &QPushButton::clicked, this, &StopMainWindow::pause);
connect(pushButton_Stop, &QPushButton::clicked, this, &StopMainWindow::stop);
pushButton_Start->setEnabled(true);
pushButton_go_on->setEnabled(false);
pushButton_Stop->setEnabled(false);
music=new QMediaPlayer(this, QMediaPlayer::StreamPlayback);
QMediaPlaylist *playlist = new QMediaPlaylist;
playlist->addMedia(QUrl("qrc:/sounds/tik.mp3"));
playlist->setPlaybackMode(QMediaPlaylist::Loop);
music->setPlaylist(playlist);
music->setVolume(100);
mRunning = false;
}
void StopMainWindow::start()
{
pushButton_Start->setEnabled(false);
pushButton_go_on->setEnabled(true);
pushButton_Stop->setEnabled(true);
music->play();
mRunning = true;
mStartTime = QDateTime::currentDateTime();
startTimer(1);
}
void StopMainWindow::pause()
{
music->pause();
mRunning = false;
pushButton_Start->setEnabled(true);
pushButton_go_on->setEnabled(false);
pushButton_Stop->setEnabled(true);
mTotalTime += mSessionTime;
}
void StopMainWindow::stop()
{
pushButton_Start->setEnabled(true);
pushButton_go_on->setEnabled(true);
pushButton_Stop->setEnabled(false);
music->stop();
mRunning = false;
}
void StopMainWindow::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event)
if(mRunning){
mSessionTime = mStartTime.msecsTo(QDateTime::currentDateTime());
qint64 time = mTotalTime + mSessionTime;
time *= 111;
unsigned int h = time / 1000 / 60 / 60;
unsigned int m = (time / 1000 / 60) - (h * 60);
unsigned int s = (time / 1000) - ((m + (h * 60))* 60);
unsigned int ms = time - (s + ((m + (h * 60))* 60)) * 1000;
const QString diff = QString("%1:%2:%3,%4").arg(h, 2, 10, QChar('0')).arg(m, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0')).arg(ms, 3, 10, QChar('0'));
mLabel->setText(diff);
}
}