Audio recorder with Qt 4.5. How to tell the recording loop to stop recording using pushbutton?
-
Hi,
I am developing a program for embedded device (Blackfin BF548). The program needs an audio recorder build in it. Currently, I am able to record sounds into an audio file. But the problem is, I cannot stop recording when I need it to. I tried QThread using the method "(here)":http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/. But the thread will only quit after while loop ends, which is never. And terminate() is a bad idea.
Any solutions?Or does anyone has similar example codes?
Please note: The embedded version of Qt I have is Qt 4.5. Thanks.
Kind regards,
Wen -
Hi and welcome to devnet,
It would be better if you could post the code you are using to record the data. Otherwise it would be pretty much crystal ball guessing.
Since you have an infinite loop, you have to add a stop slot where you set the condition to stop the loop.
@
void stop()
{
_continue = false;
}void run()
{
while (!_continue) {
// processing
}
}
@This is just a basic idea, hope it helps
-
Hi SGaist,
Here are how my codes look like.
@//======Recorder.cpp=========
//Constructor...
//Deconstructor...void Recorder::process() {
for (;;) { //recording }
emit finished();
}@@//========MainWindow.cpp=========
MainWindow::MainWindow()
{
QThread* thread = new QThread;
Recorder* recorder = new Recorder();
recorder->moveToThread(thread);
connect(recorder, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(thread, SIGNAL(started()), recorder, SLOT(process()));
connect(recorder, SIGNAL(finished()), thread, SLOT(quit()));
}void MainWindow::On_pushButton_pressed()
{
thread->start();
}void MainWindow::On_pushButton_release()
{
emit recorder->finished();
}@ -
You have to change your process() code to something like I showed in my example. Without a way to break your loop you won't be able to stop it.
Also, are you sure finished is a signal ? Anyway, you can only emit a signal from within the object that declares it.
On_pushButton_release() should call the method from Recorder that will end the loop, the rest will fall in place.
-
The example was the first thing I tried. Both approaches have the same problem that, the ui is not responding unless all the functions in process() are performed. It means the button is always being pressed and I won't be able to release it until loop end.
For example:
@void process()
{
for(int i =0; i<60000; i++)
{
if (this->stop) break;//record to file } cleanup();
}
//==MainWindow==
void on_pushButton_release()
{
recorder->stop = true;
recorder->quit();
}
@The pushbutton won't be released unitl i=59999 and finish cleanup(). Is there any method I can stop tell the loop to stop anytime before i=59999?
-
What is happening is that when started is called your infinite loop runs and "locks" the thread so it can't start it's own event loop.
Are you doing any signal/slot communication from the process function ? If not, you should rather subclass QThread, reimplement run and put your infinite loop there.
-
Yes, I link the thread.start() and recorder.process() together..But don't work very well.
Referring to the link in my first post "(the link),":http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ Subclassing QThread is wrong..Please correct me if that is wrong.
-
Read this "followup":http://woboq.com/blog/qthread-you-were-not-doing-so-wrong.html article.
Most of the time you don't need to subclass but in some cases you have to