Results 1 to 14 of 14

Thread: QThread Sound and Vibrate and Crashing

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Default QThread Sound and Vibrate and Crashing

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread Sound and Vibrate and Crashing

    Issue 1: Sound does not play at all. (Vibration works fine)
    If you use the code for sound playing in a simple test application which does only that, does it work?
    What does QSound::isAvailable () return?

    Issue 2: Application crashes on second button click event.
    On what line?

    This:
    Qt Code:
    1. objSoundThread = new SoundThread("1.wav");
    2. objSoundThread->start();
    3.  
    4. objVibrateThread = new VibrateThread(500, 1);
    5. objVibrateThread->start();
    To copy to clipboard, switch view to plain text mode 
    is spawning 2 new threads every time you click a button.
    Where do you destroy the thread objects?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread Sound and Vibrate and Crashing

    Yes Sound plays alright in test application.

    I thought thread will terminate itself once it has finished the execution. (Forgive me if i am wrong. I am 2 weeks old for Qt)

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread Sound and Vibrate and Crashing

    I thought thread will terminate itself once it has finished the execution. (Forgive me if i am wrong. I am 2 weeks old for Qt)
    This has nothing to do with Qt but basic C/C++.
    The thread will terminate, but the thread object which you allocated with 'new' needs to be symmetrically deleted with 'delete' otherwise you have a memory leakage.

    Qt Code:
    1. Yes Sound plays alright in test application.
    To copy to clipboard, switch view to plain text mode 
    Ok, next stage would be to put the playing code directly in the slot, not in a thread, see if gets played then.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread Sound and Vibrate and Crashing

    Why are you using threads?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread Sound and Vibrate and Crashing

    Why are you using threads?
    Heh... I didn't even want to go there...
    But I agree - sounds like QTimer could do just find here.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread Sound and Vibrate and Crashing

    Quote Originally Posted by high_flyer View Post
    Heh... I didn't even want to go there...
    Well, asking about threads is usually my first question as most of the time getting rid of threads also solves the main problem (like crashing).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread Sound and Vibrate and Crashing

    True.
    My assumption was that he needed threads because he wanted vibrating and playing sound in parallel.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread Sound and Vibrate and Crashing

    QSound works asynchronously and I'm assuming the feedback API is also asynchronous so no threads are required.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2011
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread Sound and Vibrate and Crashing

    Reason for using thread is basically, if the user clicks on another button while the first batch of sound and vibration is still running, then it should stop immediately and play the sound and vibration associated with second button.

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread Sound and Vibrate and Crashing

    So just use QSound::stop() on it in the slot of the other button.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #12
    Join Date
    Jan 2011
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread Sound and Vibrate and Crashing

    ok. but is there is no a similar function to stop vibration. In fact as far as i know vibration feedback does only one vibration at a given time, I have created 12 distinct vibration patterns with varying intensity and no of vibration and duration. These vibration are associated to each Ui.Button and Keypad used as hotkeys. Idea was to put them in separate threads and kill the thread whenever required.

    In the meantime i will try to dump Sound thread and use as u suggested QSound::stop() and see how it goes.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread Sound and Vibrate and Crashing

    You can just interrupt your pattern. The fact that you start the first item in the pattern doesn't mean you have to replay it until the end.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Jan 2011
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread Sound and Vibrate and Crashing

    Hi All,

    Thanks you all for directions. I have now manage to handle every issue nicely. I am sure it can be done in a better way but at the moment i am sorted.

    I playing sound asynchronously within the calling method. Vibrations is still done in separate thread because of varying different vibration patterns.

    Thanks you all very much.

    Kind Regards,
    Javed

Similar Threads

  1. Vibrate Device
    By javed_alam786 in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 4th March 2011, 19:07
  2. Crashing without debug mode- No crashing with gdb debugger
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2011, 11:27
  3. QT and sound?
    By fluffaykitties in forum Newbie
    Replies: 8
    Last Post: 29th January 2011, 09:28
  4. MP3 Sound / OGG Sound Play on GUI
    By patrik08 in forum Newbie
    Replies: 3
    Last Post: 1st September 2006, 19:01
  5. Sound on KDE 3.5
    By jaynewpeng in forum KDE Forum
    Replies: 6
    Last Post: 2nd February 2006, 13:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.