我有一个应用程序记录微。当我按下按钮时,录制开始或停止(取决于状态)。我第一次执行启动/停止但在一段时间后,应用程序在停止功能上崩溃。我似乎是随机的。
//myapp.h
class MyApp : public QObject
{
Q_OBJECT
public:
explicit MyApp(QObject *parent = 0);
~MyApp();
void createAudioRecorder();
signals:
void recordingChanged ( );
public slots:
void toogleRecord( );
private:
QAudioRecorder *myAudioRecorder;
QString pathAudioFile;
};
//myapp.cpp
MyApp::MyApp(QObject *parent) : QObject(parent)
{
createAudioRecorder();
}
MyApp::~MyApp()
{
}
void MyApp::createAudioRecorder()
{
myAudioRecorder= new QAudioRecorder(this);
pathAudioFile = QDir::currentPath();
pathAudioFile+="/audio.flac";
myAudioRecorder -> setOutputLocation ( QUrl(pathAudioFile));
QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/FLAC");
myAudioRecorder->setEncodingSettings(audioSettings);
}
void MyApp::toogleRecord(){
qDebug()<<"before, state myAudioRecorder"<<myAudioRecorder<<myAudioRecorder->state();
switch (myAudioRecorder->state()) {
case QMediaRecorder::StoppedState:
myAudioRecorder->record();
break;
case QMediaRecorder::RecordingState :
myAudioRecorder->stop(); //<------------crash here
break;
case QMediaRecorder::PausedState:
break;
default:
break;
}
qDebug()<<"after, state myAudioRecorder"<<myAudioRecorder<<myAudioRecorder->state()<<"\n";
}
我不明白它的来源。
编辑: 调试无法帮助我找到答案。
我还显示改变状态和改变状态。似乎问题在于开始录音而不是停止(在录音机状态失败之前&#39;录音&#34;
好记录:
|State | Status |
-----------------|------------|----------|
clic to start > | | |
|Recording | |
| | Starting |
| | Recording|
clic to stop > | | |
|Stopped | |
| |Finalizing|
| |Loaded |
记录期间出错:
|State | Status |
-----------------|------------|----------|
clic to start > | | |
|Recording | |
| | Starting |
clic to stop > | | |
我找到了一个防止崩溃的解决方案,但它并不漂亮。如果audioRecorder在StratingStatus或FinalizingStatus上,我重新创建AudioRecorder。
void MyApp::toogleRecord(){
switch (myAudioRecorder->status()) {
case QMediaRecorder::LoadedStatus:
myAudioRecorder->record();
break;
case QMediaRecorder::StartingStatus:
qDebug()<<"ok je suis là en starting";
createAudioRecorder();
break;
case QMediaRecorder::RecordingStatus :
myAudioRecorder->stop();
break;
case QMediaRecorder::FinalizingStatus:
qDebug()<<"ok je suis là en finalizing";
createAudioRecorder();
break;
default:
break;
}
}
如果您有其他想法,为什么audioRecording保持状态&#34;开始&#34;或者&#34;最终确定&#34;,不要犹豫。