QAudioRecorder create file



  • Hi,
    I use QAudioRecorder to get level of audio input. When I start record, a file "test.wav" is created. At the end of the application, I stop record and I want delete this file. But I can't delete it, it's seems file isn't foud.

    
    #include "testlevelaudio.h"
    #include <QDir>
    #include <QAudioFormat>
    #include <QAudioDeviceInfo>
    #include <QUrl>
    TestLevelAudio::TestLevelAudio(QObject *parent) : QObject(parent), m_microIsOn(false)
    {
        createAudioRecorder();
    }
    
    TestLevelAudio::~TestLevelAudio()
    {
        stopAudio();
        endRecordAudio();
    }
    
    void TestLevelAudio::createAudioRecorder()
    {
        m_audioRecorder = new QAudioRecorder(this);
        m_audioRecorder->setOutputLocation(QUrl::fromLocalFile(("test.wav")));
    	QAudioEncoderSettings settings;
        settings.setSampleRate(8000);
        settings.setChannelCount(1);
        settings.setCodec("audio/PCM");
        m_audioRecorder->setEncodingSettings(settings);
        m_audioRecorder->setContainerFormat("wav");
        qDebug()<<"audio recorder avec settings";
    
        m_audioProbe = new QAudioProbe(this);
        if(m_audioProbe->setSource(m_audioRecorder)) {
            connect(m_audioProbe, SIGNAL(audioBufferProbed(QAudioBuffer)),
                    this, SLOT(calculateLevel(QAudioBuffer)));
        }
        m_audioRecorder->record();
    }
    
    void TestLevelAudio::stopAudio()
    {
        qDebug()<<"stop audioInput"<<audioInput->state();
        if(audioInput->state() == QAudio::ActiveState) {
            audioInput->stop();
            outputFile->close();
        }
    }
    
    void TestLevelAudio::endRecordAudio()
    {
        if(m_audioRecorder->state() == QMediaRecorder::RecordingState) {
            qDebug()<<"demande d'arret";
            m_audioRecorder->stop();
            QFile fileToSupp((QUrl("test.wav")).toLocalFile());
            if(fileToSupp.exists())
    			fileToSupp.remove();
            else qDebug()<<"file doesn't exist";
        }
    }
    


  • Can you try if specifying an absolute path fixes the problem? My guess is that it's a working directory problem.
    Change "test.wav" into something like QDir::tempPath() + "/test.wav" in both createAudioRecorder and endRecordAudio


Log in to reply
 

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