Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. Timer for currently recording audio file

Timer for currently recording audio file

Scheduled Pinned Locked Moved Solved Brainstorm
timertimerecording
17 Posts 4 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • eyllanescE eyllanesc

    @haykarmeni Did my demo code work? If so then the problem is your implementation. If you want more help then you should provide a minimal compilable example.

    haykarmeniH Offline
    haykarmeniH Offline
    haykarmeni
    wrote on last edited by haykarmeni
    #8

    @eyllanesc here is the code(tried to minimize as much as possible to no violate compiling)

    mainwindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>1000</width>
        <height>250</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <widget class="QLabel" name="lblBackground">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>1000</width>
          <height>250</height>
         </rect>
        </property>
        <property name="text">
         <string/>
        </property>
        <property name="pixmap">
         <pixmap resource="Images.qrc">:/img/Icons/background.jpg</pixmap>
        </property>
        <property name="alignment">
         <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
        </property>
       </widget>
       <widget class="QLabel" name="lblText">
        <property name="geometry">
         <rect>
          <x>380</x>
          <y>115</y>
          <width>240</width>
          <height>20</height>
         </rect>
        </property>
        <property name="font">
         <font>
          <family>Arial</family>
          <pointsize>9</pointsize>
         </font>
        </property>
        <property name="text">
         <string>Click the button to start recording...</string>
        </property>
        <property name="alignment">
         <set>Qt::AlignCenter</set>
        </property>
       </widget>
       <widget class="QPushButton" name="btnRecord">
        <property name="geometry">
         <rect>
          <x>482</x>
          <y>200</y>
          <width>36</width>
          <height>36</height>
         </rect>
        </property>
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string/>
        </property>
        <property name="icon">
         <iconset resource="Images.qrc">
          <normaloff>:/img/Icons/microphone button.png</normaloff>:/img/Icons/microphone button.png</iconset>
        </property>
        <property name="iconSize">
         <size>
          <width>30</width>
          <height>30</height>
         </size>
        </property>
       </widget>
       <widget class="QToolButton" name="tlbtnSettings">
        <property name="geometry">
         <rect>
          <x>940</x>
          <y>200</y>
          <width>36</width>
          <height>36</height>
         </rect>
        </property>
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string/>
        </property>
        <property name="icon">
         <iconset resource="Images.qrc">
          <normaloff>:/img/Icons/settings.png</normaloff>:/img/Icons/settings.png</iconset>
        </property>
       </widget>
       <widget class="QPushButton" name="btnStop">
        <property name="geometry">
         <rect>
          <x>460</x>
          <y>200</y>
          <width>36</width>
          <height>36</height>
         </rect>
        </property>
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string/>
        </property>
        <property name="icon">
         <iconset resource="Images.qrc">
          <normaloff>:/img/Icons/stop button.png</normaloff>:/img/Icons/stop button.png</iconset>
        </property>
       </widget>
       <widget class="QPushButton" name="btnPause">
        <property name="geometry">
         <rect>
          <x>502</x>
          <y>200</y>
          <width>36</width>
          <height>36</height>
         </rect>
        </property>
        <property name="cursor">
         <cursorShape>PointingHandCursor</cursorShape>
        </property>
        <property name="text">
         <string/>
        </property>
        <property name="icon">
         <iconset resource="Images.qrc">
          <normaloff>:/img/Icons/pause button.png</normaloff>:/img/Icons/pause button.png</iconset>
        </property>
       </widget>
       <widget class="QLabel" name="lblStatus">
        <property name="geometry">
         <rect>
          <x>60</x>
          <y>30</y>
          <width>80</width>
          <height>20</height>
         </rect>
        </property>
        <property name="font">
         <font>
          <family>Arial</family>
          <pointsize>9</pointsize>
         </font>
        </property>
        <property name="text">
         <string/>
        </property>
        <property name="alignment">
         <set>Qt::AlignCenter</set>
        </property>
       </widget>
       <widget class="QLabel" name="lblDuration">
        <property name="geometry">
         <rect>
          <x>870</x>
          <y>30</y>
          <width>100</width>
          <height>20</height>
         </rect>
        </property>
        <property name="font">
         <font>
          <family>Arial</family>
          <pointsize>9</pointsize>
         </font>
        </property>
        <property name="text">
         <string/>
        </property>
        <property name="alignment">
         <set>Qt::AlignCenter</set>
        </property>
       </widget>
       <widget class="QComboBox" name="cmboxAudioInputs">
        <property name="geometry">
         <rect>
          <x>764</x>
          <y>210</y>
          <width>211</width>
          <height>25</height>
         </rect>
        </property>
       </widget>
      </widget>
     </widget>
     <resources>
      <include location="Images.qrc"/>
     </resources>
     <connections/>
    </ui>
    

    mainwindow.cpp

    void MainWindow::setupGUI(){
        ui->lblText->setStyleSheet("color: white;");
        ui->lblStatus->setStyleSheet("color: white;");
        ui->lblDuration->setStyleSheet("color:white");
        ui->btnPause->setVisible(false);
        ui->btnStop->setVisible(false);
        ui->lblDuration->setVisible(false);
        ui->cmboxAudioInputs->setVisible(false);
    }
    
    void MainWindow::on_btnRecord_clicked()
    {
        ui->btnPause->setVisible(true);//discloses Pause button
        ui->btnStop->setVisible(true);//discloses Stop button
        ui->btnRecord->setVisible(false);//hides Record button
        ui->tlbtnSettings->setVisible(false);//hides Settings toolbutton
        ui->lblText->setVisible(false);//hides "Click the button to start recording" text
        ui->lblStatus->setText("Recording");//changes status
        ui->lblDuration->setVisible(true);
    
        m_recorder = new QAudioRecorder();
        m_recorder->record();
        m_recorder->stateChanged(QMediaRecorder::State::RecordingState);//changes state of recorder
    
        m_Timer.setInterval(1000);
    
        QObject::connect(&m_Timer, &QTimer::timeout, ui->lblDuration, [this](){
            ui->lblDuration->setText(QTime(0, 0, 0).addMSecs(m_elapsedTimer.elapsed()).toString());
        });
    
        QObject::connect(ui->btnRecord, &QPushButton::clicked, &m_Timer, QOverload<>::of(&QTimer::start));
        QObject::connect(ui->btnRecord, &QPushButton::clicked, ui->lblDuration, [this](){
                                                                                         m_elapsedTimer.start();
                                                                                         ui->lblDuration->setText(QTime(0, 0, 0).addMSecs(m_elapsedTimer.elapsed()).toString());
                                                                                         });
    
        QObject::connect(ui->btnStop, &QPushButton::clicked, &m_Timer, &QTimer::stop);
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QMediaRecorder>
    #include <QAudioRecorder>
    #include <QAudioEncoderSettings>
    #include <QAudioDeviceInfo>
    #include <QTimer>
    #include <QTime>
    #include <QDebug>
    #include <QElapsedTimer>
    #include <QString>
    #include <QUrl>
    #include <QFileDialog>
    #include <QStringList>
    #include <QApplication>
    #include <QElapsedTimer>
    #include <QVBoxLayout>
    #include <QWidget>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
        void setupGUI();
    
    private slots:
        void on_btnRecord_clicked();
    
        void on_tlbtnSettings_clicked();
    
        void on_btnStop_clicked();
    
        void on_btnPause_clicked();
    
    private:
        QTimer m_Timer;
        QElapsedTimer m_elapsedTimer;
        Ui::MainWindow *ui;
        QAudioRecorder *m_recorder;
        QAudioEncoderSettings *m_settings;
    };
    #endif // MAINWINDOW_H
    

    main.cpp

    #include "mainwindow.h"
    
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • haykarmeniH haykarmeni

      @eyllanesc here is the code(tried to minimize as much as possible to no violate compiling)

      mainwindow.ui

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>MainWindow</class>
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>1000</width>
          <height>250</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>MainWindow</string>
        </property>
        <widget class="QWidget" name="centralwidget">
         <widget class="QLabel" name="lblBackground">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>1000</width>
            <height>250</height>
           </rect>
          </property>
          <property name="text">
           <string/>
          </property>
          <property name="pixmap">
           <pixmap resource="Images.qrc">:/img/Icons/background.jpg</pixmap>
          </property>
          <property name="alignment">
           <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
          </property>
         </widget>
         <widget class="QLabel" name="lblText">
          <property name="geometry">
           <rect>
            <x>380</x>
            <y>115</y>
            <width>240</width>
            <height>20</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <family>Arial</family>
            <pointsize>9</pointsize>
           </font>
          </property>
          <property name="text">
           <string>Click the button to start recording...</string>
          </property>
          <property name="alignment">
           <set>Qt::AlignCenter</set>
          </property>
         </widget>
         <widget class="QPushButton" name="btnRecord">
          <property name="geometry">
           <rect>
            <x>482</x>
            <y>200</y>
            <width>36</width>
            <height>36</height>
           </rect>
          </property>
          <property name="cursor">
           <cursorShape>PointingHandCursor</cursorShape>
          </property>
          <property name="text">
           <string/>
          </property>
          <property name="icon">
           <iconset resource="Images.qrc">
            <normaloff>:/img/Icons/microphone button.png</normaloff>:/img/Icons/microphone button.png</iconset>
          </property>
          <property name="iconSize">
           <size>
            <width>30</width>
            <height>30</height>
           </size>
          </property>
         </widget>
         <widget class="QToolButton" name="tlbtnSettings">
          <property name="geometry">
           <rect>
            <x>940</x>
            <y>200</y>
            <width>36</width>
            <height>36</height>
           </rect>
          </property>
          <property name="cursor">
           <cursorShape>PointingHandCursor</cursorShape>
          </property>
          <property name="text">
           <string/>
          </property>
          <property name="icon">
           <iconset resource="Images.qrc">
            <normaloff>:/img/Icons/settings.png</normaloff>:/img/Icons/settings.png</iconset>
          </property>
         </widget>
         <widget class="QPushButton" name="btnStop">
          <property name="geometry">
           <rect>
            <x>460</x>
            <y>200</y>
            <width>36</width>
            <height>36</height>
           </rect>
          </property>
          <property name="cursor">
           <cursorShape>PointingHandCursor</cursorShape>
          </property>
          <property name="text">
           <string/>
          </property>
          <property name="icon">
           <iconset resource="Images.qrc">
            <normaloff>:/img/Icons/stop button.png</normaloff>:/img/Icons/stop button.png</iconset>
          </property>
         </widget>
         <widget class="QPushButton" name="btnPause">
          <property name="geometry">
           <rect>
            <x>502</x>
            <y>200</y>
            <width>36</width>
            <height>36</height>
           </rect>
          </property>
          <property name="cursor">
           <cursorShape>PointingHandCursor</cursorShape>
          </property>
          <property name="text">
           <string/>
          </property>
          <property name="icon">
           <iconset resource="Images.qrc">
            <normaloff>:/img/Icons/pause button.png</normaloff>:/img/Icons/pause button.png</iconset>
          </property>
         </widget>
         <widget class="QLabel" name="lblStatus">
          <property name="geometry">
           <rect>
            <x>60</x>
            <y>30</y>
            <width>80</width>
            <height>20</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <family>Arial</family>
            <pointsize>9</pointsize>
           </font>
          </property>
          <property name="text">
           <string/>
          </property>
          <property name="alignment">
           <set>Qt::AlignCenter</set>
          </property>
         </widget>
         <widget class="QLabel" name="lblDuration">
          <property name="geometry">
           <rect>
            <x>870</x>
            <y>30</y>
            <width>100</width>
            <height>20</height>
           </rect>
          </property>
          <property name="font">
           <font>
            <family>Arial</family>
            <pointsize>9</pointsize>
           </font>
          </property>
          <property name="text">
           <string/>
          </property>
          <property name="alignment">
           <set>Qt::AlignCenter</set>
          </property>
         </widget>
         <widget class="QComboBox" name="cmboxAudioInputs">
          <property name="geometry">
           <rect>
            <x>764</x>
            <y>210</y>
            <width>211</width>
            <height>25</height>
           </rect>
          </property>
         </widget>
        </widget>
       </widget>
       <resources>
        <include location="Images.qrc"/>
       </resources>
       <connections/>
      </ui>
      

      mainwindow.cpp

      void MainWindow::setupGUI(){
          ui->lblText->setStyleSheet("color: white;");
          ui->lblStatus->setStyleSheet("color: white;");
          ui->lblDuration->setStyleSheet("color:white");
          ui->btnPause->setVisible(false);
          ui->btnStop->setVisible(false);
          ui->lblDuration->setVisible(false);
          ui->cmboxAudioInputs->setVisible(false);
      }
      
      void MainWindow::on_btnRecord_clicked()
      {
          ui->btnPause->setVisible(true);//discloses Pause button
          ui->btnStop->setVisible(true);//discloses Stop button
          ui->btnRecord->setVisible(false);//hides Record button
          ui->tlbtnSettings->setVisible(false);//hides Settings toolbutton
          ui->lblText->setVisible(false);//hides "Click the button to start recording" text
          ui->lblStatus->setText("Recording");//changes status
          ui->lblDuration->setVisible(true);
      
          m_recorder = new QAudioRecorder();
          m_recorder->record();
          m_recorder->stateChanged(QMediaRecorder::State::RecordingState);//changes state of recorder
      
          m_Timer.setInterval(1000);
      
          QObject::connect(&m_Timer, &QTimer::timeout, ui->lblDuration, [this](){
              ui->lblDuration->setText(QTime(0, 0, 0).addMSecs(m_elapsedTimer.elapsed()).toString());
          });
      
          QObject::connect(ui->btnRecord, &QPushButton::clicked, &m_Timer, QOverload<>::of(&QTimer::start));
          QObject::connect(ui->btnRecord, &QPushButton::clicked, ui->lblDuration, [this](){
                                                                                           m_elapsedTimer.start();
                                                                                           ui->lblDuration->setText(QTime(0, 0, 0).addMSecs(m_elapsedTimer.elapsed()).toString());
                                                                                           });
      
          QObject::connect(ui->btnStop, &QPushButton::clicked, &m_Timer, &QTimer::stop);
      }
      

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QMediaRecorder>
      #include <QAudioRecorder>
      #include <QAudioEncoderSettings>
      #include <QAudioDeviceInfo>
      #include <QTimer>
      #include <QTime>
      #include <QDebug>
      #include <QElapsedTimer>
      #include <QString>
      #include <QUrl>
      #include <QFileDialog>
      #include <QStringList>
      #include <QApplication>
      #include <QElapsedTimer>
      #include <QVBoxLayout>
      #include <QWidget>
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
          void setupGUI();
      
      private slots:
          void on_btnRecord_clicked();
      
          void on_tlbtnSettings_clicked();
      
          void on_btnStop_clicked();
      
          void on_btnPause_clicked();
      
      private:
          QTimer m_Timer;
          QElapsedTimer m_elapsedTimer;
          Ui::MainWindow *ui;
          QAudioRecorder *m_recorder;
          QAudioEncoderSettings *m_settings;
      };
      #endif // MAINWINDOW_H
      

      main.cpp

      #include "mainwindow.h"
      
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #9

      so, @haykarmeni and where is your connection to the timeout signal of the QTimer, to update the label? 😉


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      haykarmeniH 1 Reply Last reply
      1
      • J.HilkJ J.Hilk

        so, @haykarmeni and where is your connection to the timeout signal of the QTimer, to update the label? 😉

        haykarmeniH Offline
        haykarmeniH Offline
        haykarmeni
        wrote on last edited by
        #10

        @J-Hilk , please could you tell me what kind of code I need to add?

        eyllanescE 1 Reply Last reply
        0
        • haykarmeniH haykarmeni

          @J-Hilk , please could you tell me what kind of code I need to add?

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by
          #11

          @haykarmeni In my code there is QObject::connect(&timer, &QTimer::timeout, label, [label, &elapsedTimer](){ label->setText(QTime(0, 0, 0).addMSecs(elapsedTimer.elapsed()).toString()); }); , where is the equivalent in your code?

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          haykarmeniH 2 Replies Last reply
          2
          • eyllanescE eyllanesc

            @haykarmeni In my code there is QObject::connect(&timer, &QTimer::timeout, label, [label, &elapsedTimer](){ label->setText(QTime(0, 0, 0).addMSecs(elapsedTimer.elapsed()).toString()); }); , where is the equivalent in your code?

            haykarmeniH Offline
            haykarmeniH Offline
            haykarmeni
            wrote on last edited by
            #12
            This post is deleted!
            1 Reply Last reply
            0
            • eyllanescE eyllanesc

              @haykarmeni In my code there is QObject::connect(&timer, &QTimer::timeout, label, [label, &elapsedTimer](){ label->setText(QTime(0, 0, 0).addMSecs(elapsedTimer.elapsed()).toString()); }); , where is the equivalent in your code?

              haykarmeniH Offline
              haykarmeniH Offline
              haykarmeni
              wrote on last edited by
              #13

              @eyllanesc , I added QObject::connect(&m_Timer, &QTimer::timeout, ui->lblDuration, [this](){ ui->lblDuration->setText(QTime(0, 0, 0).addMSecs(m_elapsedTimer.elapsed()).toString()); });, but it still does not work :(

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #14

                Did you check that your lambda is called ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                haykarmeniH 1 Reply Last reply
                0
                • SGaistS SGaist

                  Did you check that your lambda is called ?

                  haykarmeniH Offline
                  haykarmeniH Offline
                  haykarmeni
                  wrote on last edited by haykarmeni
                  #15

                  @SGaist , I checked it, it works, but the strange thing was that I have to double-click on btnRecord to start recording, that is why I got lost, but actually, I don't want to have that feature, I want just a one-click event

                  eyllanescE 1 Reply Last reply
                  0
                  • haykarmeniH haykarmeni

                    @SGaist , I checked it, it works, but the strange thing was that I have to double-click on btnRecord to start recording, that is why I got lost, but actually, I don't want to have that feature, I want just a one-click event

                    eyllanescE Offline
                    eyllanescE Offline
                    eyllanesc
                    wrote on last edited by eyllanesc
                    #16

                    @haykarmeni You should point that out from the beginning, if you don't give us information it is difficult to help you. Move all connections to the constructor, also this lines:

                    m_recorder = new QAudioRecorder();
                    m_recorder->stateChanged(QMediaRecorder::State::RecordingState);//changes state of recorder
                    
                    m_Timer.setInterval(1000);
                    

                    Just leave the following in the slot:

                    void MainWindow::on_btnRecord_clicked()
                    {
                        ui->btnPause->setVisible(true);//discloses Pause button
                        ui->btnStop->setVisible(true);//discloses Stop button
                        ui->btnRecord->setVisible(false);//hides Record button
                        ui->tlbtnSettings->setVisible(false);//hides Settings toolbutton
                        ui->lblText->setVisible(false);//hides "Click the button to start recording" text
                        ui->lblStatus->setText("Recording");//changes status
                        ui->lblDuration->setVisible(true);
                        m_recorder->record();
                    }
                    

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    haykarmeniH 1 Reply Last reply
                    2
                    • eyllanescE eyllanesc

                      @haykarmeni You should point that out from the beginning, if you don't give us information it is difficult to help you. Move all connections to the constructor, also this lines:

                      m_recorder = new QAudioRecorder();
                      m_recorder->stateChanged(QMediaRecorder::State::RecordingState);//changes state of recorder
                      
                      m_Timer.setInterval(1000);
                      

                      Just leave the following in the slot:

                      void MainWindow::on_btnRecord_clicked()
                      {
                          ui->btnPause->setVisible(true);//discloses Pause button
                          ui->btnStop->setVisible(true);//discloses Stop button
                          ui->btnRecord->setVisible(false);//hides Record button
                          ui->tlbtnSettings->setVisible(false);//hides Settings toolbutton
                          ui->lblText->setVisible(false);//hides "Click the button to start recording" text
                          ui->lblStatus->setText("Recording");//changes status
                          ui->lblDuration->setVisible(true);
                          m_recorder->record();
                      }
                      
                      haykarmeniH Offline
                      haykarmeniH Offline
                      haykarmeni
                      wrote on last edited by haykarmeni
                      #17

                      thank you, @eyllanesc, now it works as I expect...thank you very much for so much assistance and readiness :)

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved