Playing low latency sounds with QSoundEffect
The project application ch11-drum-machine
displays four SoundEffectWidget
widgets: kickWidget
, snareWidget
, hihatWidget
, and crashWidget
.
Each SoundEffectWidget
widget displays a QLabel
and a QPushButton
. The label displays the sound name. If the button is clicked, a sound is played.
The Qt Multimedia module provides two main ways to play an audio file:
QMediaPlayer
: This file can play songs, movies, and Internet radio with various input formatsQSoundEffect
: This file can play low-latency.wav
files
This project example is a virtual drum machine, so we are using a QSoundEffect
object. The first step to use a QSoundEffect
is to update your .pro
file like this:
QT += core gui multimedia
Then you can initialize the sound. Here is an example:
QUrl urlKick("qrc:/sounds/kick.wav"); QUrl urlBetterKick = QUrl::fromLocalFile("/home/better-kick.wav"); QSoundEffect soundEffect; ...