How to use QSound in Qt
Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Article
Keywords: QSound
Created: mind_freak
(17 Mar 2009)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Overview
This article shows how to use QSound to asynchronously play sound files. QSound provides the simplest mechanism in Qt to access and play audio.
Preconditions
- Download and install the Qt SDK.
Function/Property
- Plays the sound stored in the file specified by the given filename
QSound *sound=new QSound("C://Documents and Settings//Viral//My Documents//sound//Windows XP Startup.wav");
sound->setLoops(3);
Signal/Slots
- Starts playing the sound specified by this QSound object.
QObject::connect(play,SIGNAL(clicked()),sound,SLOT(play()));
- Stops the sound playing.
QObject::connect(stop,SIGNAL(clicked()),sound,SLOT(stop()));
Source File
#include <QtGui/QApplication>
#include "widget.h"
#include<QSound>
#include<QPushButton>
#include<QHBoxLayout>
#include<QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *win=new QWidget();
QHBoxLayout *lay=new QHBoxLayout();
QSound *sound=new QSound("C://Documents and Settings//Viral//My Documents//sound//Windows XP Startup.wav");//getting the audio file
sound->setLoops(3);//three times that specific audio file is played
QPushButton *play=new QPushButton("PLAY");//press button to play sound
QPushButton *stop=new QPushButton("STOP");//press to stop playing
QObject::connect(play,SIGNAL(clicked()),sound,SLOT(play()));
QObject::connect(stop,SIGNAL(clicked()),sound,SLOT(stop()));
lay->addWidget(play);
lay->addWidget(stop);
win->setLayout(lay);
win->showMaximized();
win->setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ; color:rgb(255,255,255)}");
return a.exec();
}

