QTimer with setInterval(0) and QMediaPlayer: delay on playing sound
-
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
-
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. -
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
-
@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?
-
@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.