QAudioRecorder infinity loop



  • Sometimes, the recorder goes into an infinite loop.

    Recorder state: QMediaRecorder::RecordingState
    Status changed: QMediaRecorder::StartingStatus
    Status changed: QMediaRecorder::RecordingStatus
    Recorder state: QMediaRecorder::StoppedState
    Status changed: QMediaRecorder::FinalizingStatus
    Status changed: QMediaRecorder::LoadedStatus
    Recorder state: QMediaRecorder::RecordingState
    Status changed: QMediaRecorder::StartingStatus
    Status changed: QMediaRecorder::RecordingStatus
    Recorder state: QMediaRecorder::StoppedState
    Status changed: QMediaRecorder::FinalizingStatus
    Status changed: QMediaRecorder::LoadedStatus
    Recorder state: QMediaRecorder::RecordingState
    Status changed: QMediaRecorder::StartingStatus
    Status changed: QMediaRecorder::RecordingStatus
    Recorder state: QMediaRecorder::StoppedState
    Status changed: QMediaRecorder::FinalizingStatus
    Status changed: QMediaRecorder::LoadedStatus
    Recorder state: QMediaRecorder::RecordingState
    Status changed: QMediaRecorder::StartingStatus

    After that, the status should change to QMediaRecorder::RecordingStatus but it does not change and the application freezes.

    I use Qt 5.5.1 x64 on Ubuntu 14.04 x64

    Code:

    #include "audiorecorder.h"
    #include "ui_audiorecorder.h"
    #include <QSettings>
    #include <QProcess>
    
    AudioRecorder::AudioRecorder(QWidget *parent) :
      QDialog(parent),
      ui(new Ui::AudioRecorder)
    {
      ui->setupUi(this);
    
      audioRecorder = new QAudioRecorder(this);
      player = new QMediaPlayer(this);
    
      recordfile = "";
    
      connect(audioRecorder, SIGNAL(durationChanged(qint64)), this,
              SLOT(updateProgress(qint64)));
      connect(audioRecorder, SIGNAL(stateChanged(QMediaRecorder::State)), this, SLOT(onStateChanged(QMediaRecorder::State)));
      connect(audioRecorder, SIGNAL(error(QMediaRecorder::Error)), this, SLOT(onAudioError(QMediaRecorder::Error)));
      connect(audioRecorder, SIGNAL(statusChanged(QMediaRecorder::Status)), this, SLOT(statusChanged(QMediaRecorder::Status)));
      connect(player, SIGNAL(durationChanged(qint64)), this, SLOT(updateSliderRange(qint64)));
      connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
    }
    
    QString AudioRecorder::getFileName() const
    {
      if(QFile::exists(recordfile)) {
          return recordfile;
        }
      else {
          return QString();
        }
    }
    
    void AudioRecorder::onStateChanged(QMediaRecorder::State state)
    {
      qDebug() << "Recorder state: " << state;
      switch (state) {
        case QAudioRecorder::RecordingState:
          ui->recordPushButton->setText(tr("Стоп"));
          ui->pausePushButton->setText(tr("Пауза"));
          ui->pausePushButton->setEnabled(true);
          break;
        case QAudioRecorder::PausedState:
          ui->pausePushButton->setText(tr("Возобновить"));
          break;
        case QAudioRecorder::StoppedState:
          ui->recordPushButton->setText(tr("Запись"));
          ui->pausePushButton->setEnabled(false);
          break;
        }
    }
    
    AudioRecorder::~AudioRecorder()
    {
      delete player;
      delete audioRecorder;
      delete ui;
    }
    
    void AudioRecorder::on_savePushButton_clicked()
    {
      if(audioRecorder->state() != QMediaRecorder::StoppedState) {
          QMessageBox::warning(this, "Предупреждение!", "Сначала остановите запись!");
        }
      else {
          this->accept();
        }
    }
    
    void AudioRecorder::on_cancelPushButton_clicked()
    {
      this->reject();
    
      audioRecorder->stop();
      on_clearPushButton_clicked();
      recordfile = "";
    }
    
    void AudioRecorder::on_recordPushButton_clicked()
    {
      if (audioRecorder->state() != QMediaRecorder::RecordingState) {
    
          if(recordfile.isEmpty()) {
              /*TODO: show message*/
              QMessageBox::warning(this, "Предупреждение!", "Вы не выбрали имя файла в который будет сохранена запись!");
              return;
            }
    
          if(QFile::exists(recordfile)) {
              QFile::remove(recordfile);
            }
    
          QAudioEncoderSettings settings;
          settings.setCodec("audio/vorbis");
          settings.setQuality(QMultimedia::HighQuality);
    
          QString container = "ogg";
          audioRecorder->setOutputLocation(QUrl::fromLocalFile(recordfile));
    
          audioRecorder->setEncodingSettings(settings, QVideoEncoderSettings(), container);
          audioRecorder->record();
        }
      else {
          if(audioRecorder->state() == QMediaRecorder::RecordingState
             || audioRecorder->state() == QMediaRecorder::PausedState) {
              audioRecorder->stop();
              ui->progressLabel->setText(tr("Остановлено"));
            }
    
        }
    }
    
    void AudioRecorder::updateProgress(qint64 duration)
    {
      if (audioRecorder->error() != QMediaRecorder::NoError || duration < 2000)
        return;
    
      ui->progressLabel->setText(tr("Запись %1 сек").arg(duration / 1000));
    }
    
    void AudioRecorder::on_clearPushButton_clicked()
    {
      if(QFile::exists(recordfile)) {
          QFile::resize(recordfile, 0);
          ui->progressLabel->setText(tr(""));
        }
    }
    
    void AudioRecorder::on_fileToolButton_clicked()
    {
      QSettings settings;
      QString path = settings.value("audio/path", QString("/home/")).toString();
      url = QFileDialog::getSaveFileUrl(this, tr("Сохранить запись"), QUrl::fromLocalFile(path),
                                              tr("Аудио файл Ogg (*.ogg)"));
      if(url.isEmpty()) {
          return;
        }
    
      recordfile = url.toLocalFile();
      path = QFileInfo(recordfile).path();
      settings.setValue("audio/path", path);
    
      ui->fileLineEdit->setText(recordfile);
      audioRecorder->setOutputLocation(QUrl::fromLocalFile(recordfile));
    }
    
    void AudioRecorder::on_playPushButton_clicked()
    {
      if(QFile::exists(recordfile)) {
          player->setMedia(QUrl::fromLocalFile(recordfile));
          player->setVolume(90);
          player->play();
        }
      else {
          QMessageBox::warning(this, "Файл аудиозаписи", "Файла не существует, запишите его сначала!");
        }
    }
    
    void AudioRecorder::on_stopPlayPushButton_clicked()
    {
      player->stop();
      ui->horizontalSlider->setValue(0);
    }
    
    void AudioRecorder::on_pushButton_clicked()
    {
      QProcess process;
      process.start("gnome-control-center", QStringList() << "sound");
      process.waitForFinished();
    }
    
    void AudioRecorder::on_pausePushButton_clicked()
    {
      if(audioRecorder->state() == QMediaRecorder::RecordingState) {
          audioRecorder->pause();
          //ui->progressLabel->setText(tr("Пауза"));
        }
      else {
          audioRecorder->record();
        }
    }
    
    void AudioRecorder::onAudioError(QMediaRecorder::Error error)
    {
      qDebug() << "Ошибка рекордера:" << error;
    }
    
    void AudioRecorder::positionChanged(qint64 pos)
    {
      ui->horizontalSlider->setValue(pos / 1000);
    }
    
    void AudioRecorder::statusChanged(QMediaRecorder::Status status)
    {
      qDebug() << "Status changed:" << status;
    }
    
    void AudioRecorder::updateSliderRange(qint64 duration)
    {
      ui->horizontalSlider->setRange(0, duration / 1000);
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.