How to use QSound in Qt
This article shows how to use QSound to asynchronously play sound files. QSound provides the simplest mechanism in Qt to access and play audio.
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
(19 Mar 2013)
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();
}


Suriti - pro file
can you may be post the .pro file also?Suriti 23:08, 18 March 2013 (EET)
Hamishwillee - Suriti - the article was written 4 years ago, and I doubt the author is still watching it
That said, there are only 4 lines of code here - you should be able to create a new app in seconds which uses the API.
regards
Hamishhamishwillee 01:35, 19 March 2013 (EET)