0

I am trying to play a sound ,when a second chages.

In my stopwatch there are milliseconds, and it plays under miliseconds' changes, not seconds' ones. I should create a condition for *seconds*.

But can't do it correctly, can you advice this very condition?

Here is my method(stay the object of QMediaPlayer for milliseconds ):

My constructor and 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);
    }

}
Nikitax
  • 9
  • 4
  • What is the `timerEvent` period? – eyllanesc Apr 21 '17 at 13:01
  • @eyllanesc It's created for cennection with my button `countinue`. Here is my SLOT, where `timerEvent` is used: `void StopMainWindow::pause(void) { **timerEvent**(new QTimerEvent(0)); mTotalTime += mSessionTime; mRunning = false; }` – Nikitax Apr 21 '17 at 13:08
  • You could put your complete code, because it seems incorrect. – eyllanesc Apr 21 '17 at 13:11
  • @eyllanesc I've edit that, check it , please. – Nikitax Apr 21 '17 at 13:18
  • @eyllanesc it seems to me, I shouldn't play `music` in `timerEvent`. I think, I have to work with a `QDateTime::currentDateTime();` function. But I can not undestand , how I should connect it with music.. – Nikitax Apr 21 '17 at 13:21
  • The preamble of your question is a bit confusing, what I understand is that you want to control the emission of the sound with the start, stop and pause buttons, am I correct? – eyllanesc Apr 21 '17 at 13:22
  • Every few seconds you want to run `timerEvent`? – eyllanesc Apr 21 '17 at 13:23
  • @eyllanesc I would like to get that when I `clicked` START ,the sound plays under moving seconds. Sorry for making you confusing, I am trying to sort out... Just when I press `start`, the sound plays with a second. – Nikitax Apr 21 '17 at 13:25
  • So what do you use `timerEvent` for? – eyllanesc Apr 21 '17 at 13:29
  • How long is the actual duration of your music? – eyllanesc Apr 21 '17 at 13:30
  • @eyllanesc `timerEvent` changes my putting `text-numbers`.It isn't a signal, It's a function, which make the main arifmetic of `time`.The sound is `one second`. – Nikitax Apr 21 '17 at 13:36
  • Do you want that sound to be emitted for a few seconds or continuously? – eyllanesc Apr 21 '17 at 14:07
  • @eyllanesc I want that sound to be emitted every second while stopwatch is working – Nikitax Apr 21 '17 at 14:10
  • if your audio file is exactly 1 second you could just start a playlist with 1 file and keep repeating it, until pause or stop is pressed [check this answer](http://stackoverflow.com/a/37690812/7295331) – Tom Conijn Apr 21 '17 at 14:14

1 Answers1

0

I recommend initializing QMediaPlayer only once and adding the sound through QMediaPlaylist by placing QMediaPlaylist::Loop to run continuously, as the sound is an exact second does not require the use of a timer. As you want the QLabel to change every millisecond we use 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);
    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241