Hi All,
I am trying to start three threads,
First Thread need to run constantly at least once in every 3 secs. Once Button1
Second Thread is supposed to play Sound on button click
Third Thread is supposed to vibrate the handset (Nokia E52) on button click.
now 2nd and 3rd thread must run at the same time so that user gets sound and vibration feedback at the same time.
Issue 1: Sound does not play at all. (Vibration works fine)
Issue 2: Application crashes on second button click event.
Please help i have been banging my head on this for a while now.
Kind Regards
Javed
UI
---------------------------
- Button 1 -
- Button 2 -
- Button 3 -
---------------------------
QThread1.pro
SOURCES += main.cpp\
mainwindow.cpp \
VibrateThread.cpp \
ConstThread.cpp \
SoundThread.cpp
HEADERS += mainwindow.h \
SoundThread.h \
ConstThread.h \
VibrateThread.h
FORMS += mainwindow.ui
QT += network
CONFIG += mobility
MOBILITY += feedback
SOURCES += main.cpp\
mainwindow.cpp \
VibrateThread.cpp \
ConstThread.cpp \
SoundThread.cpp
HEADERS += mainwindow.h \
SoundThread.h \
ConstThread.h \
VibrateThread.h
FORMS += mainwindow.ui
QT += network
CONFIG += mobility
MOBILITY += feedback
To copy to clipboard, switch view to plain text mode
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QDialog>
#include <QTextEdit>
#include <ConstThread.h>
#include <SoundThread.h>
#include <VibrateThread.h>
namespace Ui
{
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
virtual ~MainWindow();
ConstThread *objConstThread;
SoundThread *objSoundThread;
VibrateThread *objVibrateThread;
private slots:
void on_btn1_clicked();
void on_btn2_clicked();
void on_btn3_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QDialog>
#include <QTextEdit>
#include <ConstThread.h>
#include <SoundThread.h>
#include <VibrateThread.h>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
virtual ~MainWindow();
ConstThread *objConstThread;
SoundThread *objSoundThread;
VibrateThread *objVibrateThread;
private slots:
void on_btn1_clicked();
void on_btn2_clicked();
void on_btn3_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore/QCoreApplication>
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
}
void MainWindow::on_btn1_clicked()
{
//Start Thread, must run till the application is running
objConstThread = new ConstThread();
objConstThread->IsLive = true;
objConstThread->start();
ui->btn1->setEnabled(false);
}
void MainWindow::on_btn2_clicked()
{
objSoundThread = new SoundThread("1.wav");
objSoundThread->start();
objVibrateThread = new VibrateThread(500, 1);
objVibrateThread->start();
}
void MainWindow::on_btn3_clicked()
{
objSoundThread = new SoundThread("2.wav", "3.wav");
objSoundThread->start();
objVibrateThread = new VibrateThread(500, 2);
objVibrateThread->start();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore/QCoreApplication>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
}
void MainWindow::on_btn1_clicked()
{
//Start Thread, must run till the application is running
objConstThread = new ConstThread();
objConstThread->IsLive = true;
objConstThread->start();
ui->btn1->setEnabled(false);
}
void MainWindow::on_btn2_clicked()
{
objSoundThread = new SoundThread("1.wav");
objSoundThread->start();
objVibrateThread = new VibrateThread(500, 1);
objVibrateThread->start();
}
void MainWindow::on_btn3_clicked()
{
objSoundThread = new SoundThread("2.wav", "3.wav");
objSoundThread->start();
objVibrateThread = new VibrateThread(500, 2);
objVibrateThread->start();
}
To copy to clipboard, switch view to plain text mode
ConstThread.h
#ifndef CONSTTHREAD_H
#define CONSTTHREAD_H
#include <QTextEdit>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
{
public:
ConstThread();
void run();
bool IsLive;
};
#endif // CONSTTHREAD_H
#ifndef CONSTTHREAD_H
#define CONSTTHREAD_H
#include <QTextEdit>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
class ConstThread: public QThread
{
public:
ConstThread();
void run();
bool IsLive;
};
#endif // CONSTTHREAD_H
To copy to clipboard, switch view to plain text mode
ConstThread.cpp
#include "ConstThread.h"
ConstThread::ConstThread()
{
}
void ConstThread::run()
{
qint64 counter = 0;
while (IsLive == true)
{
counter++;
if ((counter%100) == 0)
{
counter = 0;
// do somthing
}
else
{
//do somthing
}
this->msleep(3000);
}
}
#include "ConstThread.h"
ConstThread::ConstThread()
{
}
void ConstThread::run()
{
qint64 counter = 0;
while (IsLive == true)
{
counter++;
if ((counter%100) == 0)
{
counter = 0;
// do somthing
}
else
{
//do somthing
}
this->msleep(3000);
}
}
To copy to clipboard, switch view to plain text mode
SoundThread.h
#ifndef SOUNDTHREAD_H
#define SOUNDTHREAD_H
#include <QTextEdit>
#include <QSound>
#include <QTime>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
{
public:
SoundThread
(QString wavFileName1Val
);
void run();
};
#endif // SOUNDTHREAD_H
#ifndef SOUNDTHREAD_H
#define SOUNDTHREAD_H
#include <QTextEdit>
#include <QSound>
#include <QTime>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
class SoundThread: public QThread
{
public:
SoundThread(QString wavFileName1Val);
SoundThread(QString wavFileName1Val, QString wavFileName2Val);
void run();
QString wavFileName1;
QString wavFileName2;
};
#endif // SOUNDTHREAD_H
To copy to clipboard, switch view to plain text mode
SoundThread.cpp
#include "SoundThread.h"
SoundThread
::SoundThread(QString wavFileName1Val
){
wavFileName1 = wavFileName1Val;
wavFileName2 = "";
}
SoundThread
::SoundThread(QString wavFileName1Val,
QString wavFileName2Val
){
wavFileName1 = wavFileName1Val;
wavFileName2 = wavFileName2Val;
}
void SoundThread::run()
{
QSound::play("C:\\Data\\Sounds\\TennisMobile\\AdvantageReceiver.wav");
if (wavFileName2 != "")
{
//All sound are 1sec long
waitCondition.wait(&dummy, 1010);
QSound::play("C:\\Data\\Sounds\\TennisMobile\\Let.wav");
}
this->msleep(2000);
}
#include "SoundThread.h"
SoundThread::SoundThread(QString wavFileName1Val)
{
wavFileName1 = wavFileName1Val;
wavFileName2 = "";
}
SoundThread::SoundThread(QString wavFileName1Val, QString wavFileName2Val)
{
wavFileName1 = wavFileName1Val;
wavFileName2 = wavFileName2Val;
}
void SoundThread::run()
{
QMutex dummy;
QMutexLocker locker(&dummy);
QWaitCondition waitCondition;
QSound::play("C:\\Data\\Sounds\\TennisMobile\\AdvantageReceiver.wav");
if (wavFileName2 != "")
{
//All sound are 1sec long
waitCondition.wait(&dummy, 1010);
QSound::play("C:\\Data\\Sounds\\TennisMobile\\Let.wav");
}
this->msleep(2000);
}
To copy to clipboard, switch view to plain text mode
VibrateThread.h
#ifndef VIBRATETHREAD_H
#define VIBRATETHREAD_H
#include <qmobilityglobal.h>
#include <qfeedbackActuator.h>
#include <qfeedbackeffect.h>
#include <qfeedbackplugininterfaces.h>
#include <QTextEdit>
#include <QSound>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
class VibrateThread
: public QThread{
public:
VibrateThread(int msecVal, int noOfVibsVal);
void run();
qint32 qintMSec;
qint8 qint8NoOfVibs;
qint16 qint16Interval;
QtMobility::QFeedbackHapticsEffect rumble;
QtMobility::QFeedbackActuator *vibra;
};
#endif // VIBRATETHREAD_H
#ifndef VIBRATETHREAD_H
#define VIBRATETHREAD_H
#include <qmobilityglobal.h>
#include <qfeedbackActuator.h>
#include <qfeedbackeffect.h>
#include <qfeedbackplugininterfaces.h>
#include <QTextEdit>
#include <QSound>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
class VibrateThread: public QThread
{
public:
VibrateThread(int msecVal, int noOfVibsVal);
void run();
qint32 qintMSec;
qint8 qint8NoOfVibs;
qint16 qint16Interval;
QtMobility::QFeedbackHapticsEffect rumble;
QtMobility::QFeedbackActuator *vibra;
};
#endif // VIBRATETHREAD_H
To copy to clipboard, switch view to plain text mode
VibrateThread.cpp
#include "VibrateThread.h"
VibrateThread::VibrateThread(int msecVal, int noOfVibsVal)
{
qintMSec = msecVal;
qint8NoOfVibs = noOfVibsVal;
qint16Interval = 100;
QList<QtMobility::QFeedbackActuator *> list = QtMobility::QFeedbackActuator::actuators();
vibra = 0;
foreach(QtMobility::QFeedbackActuator *a, list)
{
if(a->name() == "Vibra")
vibra = a;
}
if (vibra == 0)
{ }
}
void VibrateThread::run()
{
rumble.setIntensity(1.00);
rumble.setDuration(qintMSec);
rumble.setActuator(vibra);
rumble.start();
this->msleep(qintMSec);
this->msleep(100);//wait msec between two vibrations
for (int count = 1 ; count < qint8NoOfVibs ; count++)
{
rumble.start();
this->msleep(qintMSec + 100); //vibration time + wait time before second vibration
}
this->wait();
}
#include "VibrateThread.h"
VibrateThread::VibrateThread(int msecVal, int noOfVibsVal)
{
qintMSec = msecVal;
qint8NoOfVibs = noOfVibsVal;
qint16Interval = 100;
QList<QtMobility::QFeedbackActuator *> list = QtMobility::QFeedbackActuator::actuators();
vibra = 0;
foreach(QtMobility::QFeedbackActuator *a, list)
{
if(a->name() == "Vibra")
vibra = a;
}
if (vibra == 0)
{ }
}
void VibrateThread::run()
{
rumble.setIntensity(1.00);
rumble.setDuration(qintMSec);
rumble.setActuator(vibra);
rumble.start();
this->msleep(qintMSec);
this->msleep(100);//wait msec between two vibrations
for (int count = 1 ; count < qint8NoOfVibs ; count++)
{
rumble.start();
this->msleep(qintMSec + 100); //vibration time + wait time before second vibration
}
this->wait();
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "mainwindow.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
MainWindow mainWindow;
#if defined(Q_WS_S60)
mainWindow.showMaximized();
#else
mainWindow.show();
#endif
return app.exec();
}
#include "mainwindow.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
#if defined(Q_WS_S60)
mainWindow.showMaximized();
#else
mainWindow.show();
#endif
return app.exec();
}
To copy to clipboard, switch view to plain text mode
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>179</width>
<height>138</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="btn2">
<property name="geometry">
<rect>
<x>10</x>
<y>50</y>
<width>151</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Button 2 Sound & Vibrate</string>
</property>
</widget>
<widget class="QPushButton" name="btn3">
<property name="geometry">
<rect>
<x>10</x>
<y>80</y>
<width>151</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Button 3 Sound & Vibrate</string>
</property>
</widget>
<widget class="QPushButton" name="btn1">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>151</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Button 1 Looper</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>179</width>
<height>21</height>
</rect>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</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>179</width>
<height>138</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="btn2">
<property name="geometry">
<rect>
<x>10</x>
<y>50</y>
<width>151</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Button 2 Sound & Vibrate</string>
</property>
</widget>
<widget class="QPushButton" name="btn3">
<property name="geometry">
<rect>
<x>10</x>
<y>80</y>
<width>151</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Button 3 Sound & Vibrate</string>
</property>
</widget>
<widget class="QPushButton" name="btn1">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>151</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Button 1 Looper</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>179</width>
<height>21</height>
</rect>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
To copy to clipboard, switch view to plain text mode
Bookmarks