Audioplayer code looks something like this is according to before long a post do tower defense game
Audio how all put out both the background music and special effects sound is
# include "audioplayer. H"//the corresponding header file
#include
#include
#include
The static const QString s_curDir=QDir: : currentPath () + "/".
AudioPlayer: : AudioPlayer (QObject * parent)
: QObject (parent)
, m_backgroundMusic (0)
{
//create always play background music
QUrl backgroundMusicUrl=QUrl: : fromLocalFile (s_curDir + "music/8 bitdungeonlevel. Mp3");//file the location of the
If (QFile: : the exists (backgroundMusicUrl toLocalFile ()))//if there is a return 1
{
M_backgroundMusic=new QMediaPlayer (this);//points to the object itself address
QMediaPlaylist * backgroundMusicList=new QMediaPlaylist ();//the list also points to the address of the default class object itself this
QMediaContent media (backgroundMusicUrl);//media for backgroundMusicUrl object reference
BackgroundMusicList - & gt; AddMedia (media);//list insert
BackgroundMusicList - & gt; SetCurrentIndex (0);//switch music began to play
//set the background music looping
BackgroundMusicList - & gt; SetPlaybackMode (QMediaPlaylist: : CurrentItemInLoop);//play mode looping
M_backgroundMusic - & gt; SetPlaylist (backgroundMusicList);//will list guide m_backgroundMusic
}
}
Void AudioPlayer: : startBGM ()
{
If (m_backgroundMusic)
M_backgroundMusic - & gt; play();//DJ djing
}
Void AudioPlayer: : playSound (SoundType SoundType)
{
The static const QUrl mediasUrls []=
{
QUrl: : fromLocalFile (s_curDir + "music/tower_place. Wav"),
QUrl: : fromLocalFile (s_curDir + "music/life_lose. Wav"),
QUrl: : fromLocalFile (s_curDir + "music/laser_shoot. Wav"),
QUrl: : fromLocalFile (s_curDir + "music/enemy_destroy. Wav")
};//url in mediasUrls []
The static QMediaPlayer player;//will set Qmediaplayer player object
If (QFile: : the exists (mediasUrls [soundType] toLocalFile ()))//if there is a return 1
{
Player. SetMedia (mediasUrls [soundType]);//link list
Player, play ();//start playing
}
}