Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. QTimer with setInterval(0) and QMediaPlayer: delay on playing sound
Forum Updated to NodeBB v4.3 + New Features

QTimer with setInterval(0) and QMediaPlayer: delay on playing sound

Scheduled Pinned Locked Moved Unsolved Game Development
6 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Morfio
    wrote on last edited by
    #1

    Hello,

    I fire a QTimer with an interval of zero. Every time I press space, a sound should be played in background (a "plop" sound) like a shoot. The first time I shoot it's played directly. After that sometimes it doesn't play or it plays one or two seconds later.

    If I use an interval of 20 milliseconds it plays right, but my game stutters. With no timer on it works, too.

    Here is my code for the playing:

    if(plopMediaPlayer.state() == QMediaPlayer::PlayingState)
    					plopMediaPlayer.setPosition(0);
    				else
    					plopMediaPlayer.play();
    

    How am I able to play the sound directly with zero interval timer?

    Thank you all and sorry about my bad english

    Morfio

    K 1 Reply Last reply
    0
    • M Morfio

      Hello,

      I fire a QTimer with an interval of zero. Every time I press space, a sound should be played in background (a "plop" sound) like a shoot. The first time I shoot it's played directly. After that sometimes it doesn't play or it plays one or two seconds later.

      If I use an interval of 20 milliseconds it plays right, but my game stutters. With no timer on it works, too.

      Here is my code for the playing:

      if(plopMediaPlayer.state() == QMediaPlayer::PlayingState)
      					plopMediaPlayer.setPosition(0);
      				else
      					plopMediaPlayer.play();
      

      How am I able to play the sound directly with zero interval timer?

      Thank you all and sorry about my bad english

      Morfio

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Morfio

      Why do you want to use a QTimer if you do not apply a delay?
      IMHO this is the only reason for having a timer is the application of some delay.

      @Morfio said in QTimer with setInterval(0) and QMediaPlayer: delay on playing sound:

      If I use an interval of 20 milliseconds it plays right, but my game stutters. With no timer on it works, too.

      Does your application also stutter with no timer?
      If this is the case there is probably another reason for the delay. For instance if you play a sound this may take time and if the repetition is too often this may slow your application or any other reason similar may slow down your application.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Morfio
        wrote on last edited by
        #3

        Hi,

        thanks for reply.

        If I use a normal game loop, I have the same issues. I try to make an easy example of (what I think) a strange behavior.

        main.cpp

        #include "MainWindow.h"
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
        	QApplication a(argc, argv);
        	MainWindow w;
        	w.show();
        	w.run();
        
        	return a.exec();
        }
        

        MainWindow.h

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        #include <QMediaPlayer>
        #include <QKeyEvent>
        #include <QCloseEvent>
        
        namespace Ui {
        	class MainWindow;
        }
        
        class MainWindow : public QMainWindow {
        
        	Q_OBJECT
        
        	public:
        		explicit MainWindow(QWidget *parent = nullptr);
        		~MainWindow() override;
        
        		void keyPressEvent(QKeyEvent *event) override;
        		void run();
        		void closeEvent(QCloseEvent *event) override;
        
        	private:
        		Ui::MainWindow *ui;
        		QMediaPlayer mediaPlayer;
        		void doRender();
        		bool stop;
        
        };
        
        #endif // MAINWINDOW_H
        

        MainWindow.cpp

        #include "MainWindow.h"
        #include "ui_MainWindow.h"
        #include <QKeyEvent>
        #include <QGuiApplication>
        #include <QThread>
        #include <QDebug>
        
        MainWindow::MainWindow(
        	QWidget *parent
        ) : QMainWindow(
        	parent
        ), ui(
        	new Ui::MainWindow
        ), stop(
        	false
        ) {
        	ui->setupUi(this);
        
        	mediaPlayer.setMedia(QUrl("qrc:/Plop.wav"));
        }
        
        MainWindow::~MainWindow() {
        	delete ui;
        }
        
        void MainWindow::keyPressEvent(QKeyEvent *event) {
        	switch(event->key()) {
        		case Qt::Key_Space:
        			if(mediaPlayer.state() == QMediaPlayer::PlayingState)
        				mediaPlayer.setPosition(0);
        			else
        				mediaPlayer.play();
        			break;
        	}
        }
        
        void MainWindow::doRender() {
        	QThread::msleep(100);
        }
        
        void MainWindow::run() {
        	while(!stop) {
        		QGuiApplication::processEvents();
        		doRender();
        	}
        }
        
        void MainWindow::closeEvent(QCloseEvent *event) {
        	qDebug() << "stop";
        
        	stop = true;
        }
        

        I've uploaded the whole source to https://cloud.gug-it.de/index.php/s/DTnmHZXSjTQLz5n because here I have no permissions to upload files.

        The behavior is that the sound is not played all the time I press space. doRender() is just an example to show the rendering time of my game.

        Maybe I don't understand well, how QMediaPlayer works, but shouldn't it play the sound in background?

        Thank you

        Morfio

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Morfio
          wrote on last edited by
          #4

          Hi everybody,

          it seems to be a FreeBSD specific problem. I tried it under macOS and Windows. On these platforms the sound works great.

          Morfio

          K 1 Reply Last reply
          0
          • M Morfio

            Hi everybody,

            it seems to be a FreeBSD specific problem. I tried it under macOS and Windows. On these platforms the sound works great.

            Morfio

            K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            @Morfio said in QTimer with setInterval(0) and QMediaPlayer: delay on playing sound:

            Hi everybody,

            it seems to be a FreeBSD specific problem. I tried it under macOS and Windows. On these platforms the sound works great.

            Morfio

            Might be also processor related. Or do you run all tests on exactly the same processor board?

            Vote the answer(s) that helped you to solve your issue(s)

            M 1 Reply Last reply
            0
            • K koahnig

              @Morfio said in QTimer with setInterval(0) and QMediaPlayer: delay on playing sound:

              Hi everybody,

              it seems to be a FreeBSD specific problem. I tried it under macOS and Windows. On these platforms the sound works great.

              Morfio

              Might be also processor related. Or do you run all tests on exactly the same processor board?

              M Offline
              M Offline
              Morfio
              wrote on last edited by
              #6

              @koahnig No, I didn't. I tried it now under a MacBook Pro and an Android device (Xiaomi Mi Max 2). It works great.

              I think the issue is related to the OSS and gstreamer backend of FreeBSD. There seems to be some problems. Sometimes gstreamer loses the connection to the audio device. Maybe a gestreamer or Qt bug on a not official supported platform.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved