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
Qt Code:
  1. SOURCES += main.cpp\
  2. mainwindow.cpp \
  3. VibrateThread.cpp \
  4. ConstThread.cpp \
  5. SoundThread.cpp
  6.  
  7. HEADERS += mainwindow.h \
  8. SoundThread.h \
  9. ConstThread.h \
  10. VibrateThread.h
  11.  
  12. FORMS += mainwindow.ui
  13.  
  14. QT += network
  15. CONFIG += mobility
  16. MOBILITY += feedback
To copy to clipboard, switch view to plain text mode 


MainWindow.h
Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QtGui/QMainWindow>
  5. #include <QDialog>
  6. #include <QTextEdit>
  7.  
  8. #include <ConstThread.h>
  9. #include <SoundThread.h>
  10. #include <VibrateThread.h>
  11.  
  12.  
  13. namespace Ui
  14. {
  15. class MainWindow;
  16. }
  17.  
  18. class MainWindow : public QMainWindow
  19. {
  20. Q_OBJECT
  21. public:
  22. explicit MainWindow(QWidget *parent = 0);
  23. virtual ~MainWindow();
  24.  
  25. ConstThread *objConstThread;
  26. SoundThread *objSoundThread;
  27. VibrateThread *objVibrateThread;
  28.  
  29. private slots:
  30.  
  31. void on_btn1_clicked();
  32.  
  33. void on_btn2_clicked();
  34.  
  35. void on_btn3_clicked();
  36.  
  37. private:
  38. Ui::MainWindow *ui;
  39. };
  40.  
  41. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 

MainWindow.cpp
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. #include <QtCore/QCoreApplication>
  5.  
  6. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9. }
  10.  
  11. MainWindow::~MainWindow()
  12. {
  13.  
  14. }
  15.  
  16. void MainWindow::on_btn1_clicked()
  17. {
  18. //Start Thread, must run till the application is running
  19. objConstThread = new ConstThread();
  20. objConstThread->IsLive = true;
  21. objConstThread->start();
  22.  
  23. ui->btn1->setEnabled(false);
  24. }
  25.  
  26. void MainWindow::on_btn2_clicked()
  27. {
  28. objSoundThread = new SoundThread("1.wav");
  29. objSoundThread->start();
  30.  
  31. objVibrateThread = new VibrateThread(500, 1);
  32. objVibrateThread->start();
  33. }
  34.  
  35. void MainWindow::on_btn3_clicked()
  36. {
  37. objSoundThread = new SoundThread("2.wav", "3.wav");
  38. objSoundThread->start();
  39.  
  40. objVibrateThread = new VibrateThread(500, 2);
  41. objVibrateThread->start();
  42. }
To copy to clipboard, switch view to plain text mode 


ConstThread.h
Qt Code:
  1. #ifndef CONSTTHREAD_H
  2. #define CONSTTHREAD_H
  3.  
  4. #include <QTextEdit>
  5. #include <QThread>
  6. #include <QMutex>
  7. #include <QWaitCondition>
  8.  
  9. class ConstThread: public QThread
  10. {
  11. public:
  12. ConstThread();
  13. void run();
  14.  
  15. bool IsLive;
  16. };
  17.  
  18. #endif // CONSTTHREAD_H
To copy to clipboard, switch view to plain text mode 

ConstThread.cpp
Qt Code:
  1. #include "ConstThread.h"
  2.  
  3. ConstThread::ConstThread()
  4. {
  5. }
  6.  
  7. void ConstThread::run()
  8. {
  9. qint64 counter = 0;
  10.  
  11. while (IsLive == true)
  12. {
  13. counter++;
  14. if ((counter%100) == 0)
  15. {
  16. counter = 0;
  17. // do somthing
  18. }
  19. else
  20. {
  21. //do somthing
  22. }
  23.  
  24. this->msleep(3000);
  25. }
  26. }
To copy to clipboard, switch view to plain text mode 

SoundThread.h
Qt Code:
  1. #ifndef SOUNDTHREAD_H
  2. #define SOUNDTHREAD_H
  3.  
  4. #include <QTextEdit>
  5. #include <QSound>
  6. #include <QTime>
  7.  
  8. #include <QThread>
  9. #include <QMutex>
  10. #include <QWaitCondition>
  11.  
  12. class SoundThread: public QThread
  13. {
  14. public:
  15. SoundThread(QString wavFileName1Val);
  16. SoundThread(QString wavFileName1Val, QString wavFileName2Val);
  17. void run();
  18.  
  19. QString wavFileName1;
  20. QString wavFileName2;
  21. };
  22.  
  23. #endif // SOUNDTHREAD_H
To copy to clipboard, switch view to plain text mode 

SoundThread.cpp
Qt Code:
  1. #include "SoundThread.h"
  2.  
  3. SoundThread::SoundThread(QString wavFileName1Val)
  4. {
  5. wavFileName1 = wavFileName1Val;
  6. wavFileName2 = "";
  7. }
  8.  
  9. SoundThread::SoundThread(QString wavFileName1Val, QString wavFileName2Val)
  10. {
  11. wavFileName1 = wavFileName1Val;
  12. wavFileName2 = wavFileName2Val;
  13. }
  14.  
  15. void SoundThread::run()
  16. {
  17.  
  18. QMutex dummy;
  19. QMutexLocker locker(&dummy);
  20. QWaitCondition waitCondition;
  21.  
  22. QSound::play("C:\\Data\\Sounds\\TennisMobile\\AdvantageReceiver.wav");
  23. if (wavFileName2 != "")
  24. {
  25. //All sound are 1sec long
  26. waitCondition.wait(&dummy, 1010);
  27. QSound::play("C:\\Data\\Sounds\\TennisMobile\\Let.wav");
  28. }
  29. this->msleep(2000);
  30. }
To copy to clipboard, switch view to plain text mode 

VibrateThread.h
Qt Code:
  1. #ifndef VIBRATETHREAD_H
  2. #define VIBRATETHREAD_H
  3.  
  4. #include <qmobilityglobal.h>
  5. #include <qfeedbackActuator.h>
  6. #include <qfeedbackeffect.h>
  7. #include <qfeedbackplugininterfaces.h>
  8.  
  9. #include <QTextEdit>
  10.  
  11. #include <QSound>
  12. #include <QThread>
  13. #include <QMutex>
  14. #include <QWaitCondition>
  15.  
  16. class VibrateThread: public QThread
  17. {
  18. public:
  19. VibrateThread(int msecVal, int noOfVibsVal);
  20. void run();
  21.  
  22. qint32 qintMSec;
  23. qint8 qint8NoOfVibs;
  24. qint16 qint16Interval;
  25.  
  26. QtMobility::QFeedbackHapticsEffect rumble;
  27. QtMobility::QFeedbackActuator *vibra;
  28. };
  29.  
  30. #endif // VIBRATETHREAD_H
To copy to clipboard, switch view to plain text mode 

VibrateThread.cpp
Qt Code:
  1. #include "VibrateThread.h"
  2.  
  3. VibrateThread::VibrateThread(int msecVal, int noOfVibsVal)
  4. {
  5. qintMSec = msecVal;
  6. qint8NoOfVibs = noOfVibsVal;
  7. qint16Interval = 100;
  8.  
  9. QList<QtMobility::QFeedbackActuator *> list = QtMobility::QFeedbackActuator::actuators();
  10.  
  11. vibra = 0;
  12.  
  13. foreach(QtMobility::QFeedbackActuator *a, list)
  14. {
  15. if(a->name() == "Vibra")
  16. vibra = a;
  17. }
  18.  
  19. if (vibra == 0)
  20. { }
  21. }
  22.  
  23. void VibrateThread::run()
  24. {
  25. rumble.setIntensity(1.00);
  26. rumble.setDuration(qintMSec);
  27. rumble.setActuator(vibra);
  28. rumble.start();
  29. this->msleep(qintMSec);
  30. this->msleep(100);//wait msec between two vibrations
  31.  
  32. for (int count = 1 ; count < qint8NoOfVibs ; count++)
  33. {
  34. rumble.start();
  35. this->msleep(qintMSec + 100); //vibration time + wait time before second vibration
  36. }
  37.  
  38. this->wait();
  39. }
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include "mainwindow.h"
  2.  
  3. #include <QtGui/QApplication>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication app(argc, argv);
  8.  
  9. MainWindow mainWindow;
  10.  
  11. #if defined(Q_WS_S60)
  12. mainWindow.showMaximized();
  13. #else
  14. mainWindow.show();
  15. #endif
  16.  
  17. return app.exec();
  18. }
To copy to clipboard, switch view to plain text mode 

mainwindow.ui
Qt Code:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>MainWindow</class>
  4. <widget class="QMainWindow" name="MainWindow">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>179</width>
  10. <height>138</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>MainWindow</string>
  15. </property>
  16. <widget class="QWidget" name="centralWidget">
  17. <widget class="QPushButton" name="btn2">
  18. <property name="geometry">
  19. <rect>
  20. <x>10</x>
  21. <y>50</y>
  22. <width>151</width>
  23. <height>23</height>
  24. </rect>
  25. </property>
  26. <property name="text">
  27. <string>Button 2 Sound &amp; Vibrate</string>
  28. </property>
  29. </widget>
  30. <widget class="QPushButton" name="btn3">
  31. <property name="geometry">
  32. <rect>
  33. <x>10</x>
  34. <y>80</y>
  35. <width>151</width>
  36. <height>23</height>
  37. </rect>
  38. </property>
  39. <property name="text">
  40. <string>Button 3 Sound &amp; Vibrate</string>
  41. </property>
  42. </widget>
  43. <widget class="QPushButton" name="btn1">
  44. <property name="geometry">
  45. <rect>
  46. <x>10</x>
  47. <y>20</y>
  48. <width>151</width>
  49. <height>23</height>
  50. </rect>
  51. </property>
  52. <property name="text">
  53. <string>Button 1 Looper</string>
  54. </property>
  55. </widget>
  56. </widget>
  57. <widget class="QMenuBar" name="menuBar">
  58. <property name="geometry">
  59. <rect>
  60. <x>0</x>
  61. <y>0</y>
  62. <width>179</width>
  63. <height>21</height>
  64. </rect>
  65. </property>
  66. </widget>
  67. </widget>
  68. <layoutdefault spacing="6" margin="11"/>
  69. <resources/>
  70. <connections/>
  71. </ui>
To copy to clipboard, switch view to plain text mode