QmediaPlayer in another Thread does not reproduce any sound



  • Hello everyone,
    I did a search in the forum but I did not find anything about it.
    I made a program to simulate a slot machine.
    Everything works more or less well if it was not that the sound of the slot I put is not played in the scondario thread.
    Documenting I saw that in order to be able to play the sound without interrupting the execution of the main thread is to use a secondary thread.
    I tested the thing under Windows and it works, but under Linux there is no way to reproduce the sound.
    This is wath i wrote:

    mainWindow.h

    ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QTimer>
    #include <QSound>
    #include <QThread>
    #include <QMediaPlayer>
    #include <QMediaPlaylist>
    #include "ritardo.h"
    #include "roller.h"
    
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
        int getRand(int,int);
    
        void on_pushButton_2_clicked();
    
    private:
        Ui::MainWindow *ui;
        WorkerThread *workerThread;
    
    };
    
    #endif // MAINWINDOW_H
    

    roller.h

    ifndef ROLLER_H
    #define ROLLER_H
    #include <QThread>
    #include <QMediaPlayer>
    #include <QFileInfo>
    #include <QSound>
    
    
    
    class WorkerThread : public QThread
    {
        Q_OBJECT
        void run() override {
            QString result;
    
            this->playerM = new QMediaPlayer();
            this->playerM->setMedia(QUrl("file:///home/lemoeb/sound/roll.mp3"));
            
            this->playerM->setVolume(100);
         
            emit resultReady(result);
        }
    
    signals:
        void resultReady(const QString &s);
    private:
        QMediaPlayer *playerM;
        QSound *test;
    public:
        WorkerThread(){};
        ~WorkerThread(){
    
        }
        void stopPlayer(){
            this->playerM->stop();
    
        }
        void startPlayer(){
            this->playerM->play();
        }
    
    
    };
    
    
    #endif // ROLLER_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QTime>
    
    
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
    
        ui->setupUi(this);
        workerThread = new WorkerThread();
        workerThread->start();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    int MainWindow::getRand(int low,int high){
    
        return qrand() % ((high + 1) - low) + low;
    }
    
    
    
    void MainWindow::on_pushButton_clicked()
    {
    
        int a,b,c,d=0;
        int stop1,stop2,stop3,stop4=0;
        int exit=0;
    
       workerThread->startPlayer();
    
    
        QTime time = QTime::currentTime();
        qsrand((uint)time.msec());
        for (int i=500;i>1;i--){
            if (i>470){
                exit=0;
              while (exit==0){
                 int n=getRand(1,9);
                 if (a!=n){
                     a=n;
                     exit=1;
                 }
              }
              QString immagine=QString::number(getRand(1,9));
              QPixmap pixmap("./image/" + immagine +".png");
              ui->label1->setPixmap(pixmap);
              ui->label1->repaint();
    
    
            }
            else
            {
                if (stop1==0){
    
                    stop1=1;
                }
            }
            if (i>440){
              exit=0;
              while (exit==0){
                 int n=getRand(1,9);
                 if (b!=n){
                     b=n;
                     exit=1;
                 }
              }
             // b=getRand(1,9);
    
              QPixmap pixmap2("./image/" + QString::number(getRand(1,9)) +".png");
              ui->label2->setPixmap(pixmap2);
              ui->label2->repaint();
            }
            if (i>410){
              //c=getRand(1,9);
              exit=0;
              while (exit==0){
                 int n=getRand(1,9);
                 if (c!=n){
                     c=n;
                     exit=1;
                 }
              }
              QPixmap pixmap3("./image/" + QString::number(getRand(1,9)) +".png");
              ui->label3->setPixmap(pixmap3);
              ui->label3->repaint();
            }
            if (i>380){
              d=getRand(1,9);
              exit=0;
              while (exit==0){
                 int n=getRand(1,9);
                 if (d!=n){
                     d=n;
                     exit=1;
                 }
              }
              QPixmap pixmap4("./image/" + QString::number(getRand(1,9)) +".png");
              ui->label4->setPixmap(pixmap4);
              ui->label4->repaint();
            }
            ui->lblCount->setText(QString::number(i));
            if (i>380){
                QTime timeStart = QTime::currentTime();
                QTime timeStop=timeStart.addMSecs(50);
                while (timeStart < timeStop){
                    timeStart = QTime::currentTime();
                }
            }
            else
            {
                //this->workerThread->stopPlayer();
                ui->lblCount->setText("STOP");
            }
    
    
        }
    
    
        //QPixmap pixmap("./image/" + immagine +".png");
    
    }
    
    void MainWindow::on_pushButton_2_clicked()
    {
        //QSound::play("./sound/stop.wav");
    
       // roll.play();
    }
    

    SlotMachine.pro

    #
    # Project created by QtCreator 2017-12-11T22:07:06
    #
    #-------------------------------------------------
    
    QT       += core gui
    QT += multimedia
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = SlotMachine
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    
    SOURCES += \
            main.cpp \
            mainwindow.cpp
    
    HEADERS += \
            mainwindow.h \
        ritardo.h \
        roller.h
    
    FORMS += \
            mainwindow.ui
    

    This problem is presente only in Linux Machine.

    Can anyone help me about it?

    Thank you in advance.

    Leandro


  • Lifetime Qt Champion

    Hi,

    You should rather use QUrl::fromLocalFile.

    As for your worker thread, it doesn't do what you expect. You don't have an event loop running.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.