Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions qge/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ Sound::Sound(std::string filePath, QObject *parent):
QObject(parent)
{
// initialize
audioOutput_ = new QAudioOutput;
mediaPlayer_ = new QMediaPlayer(this);
mediaPlayer_->setMedia(QUrl(filePath.c_str()));
mediaPlayer_->setAudioOutput(audioOutput_);
mediaPlayer_->setSource (QUrl(filePath.c_str()));

numTimesPlayed_ = 0;
numTimesToPlay_ = 0;

connect(mediaPlayer_,&QMediaPlayer::stateChanged,this,&Sound::stateChanged_);
connect(mediaPlayer_,&QMediaPlayer::playbackStateChanged,this,&Sound::stateChanged_);
}

/// Plays the sound the specified number of times. Pass -1 to play an infinite
Expand All @@ -23,7 +25,7 @@ void Sound::play(int numOfTimes)
numTimesToPlay_ = numOfTimes;
numTimesPlayed_ = 1;

if (mediaPlayer_->state() == QMediaPlayer::PlayingState){
if (mediaPlayer_->playbackState() == QMediaPlayer::PlayingState){
mediaPlayer_->stop();
}

Expand All @@ -41,25 +43,25 @@ void Sound::stop()
/// Sets the volume of the sound, from 0-100.
void Sound::setVolume(int volume)
{
mediaPlayer_->setVolume(volume);
audioOutput_->setVolume(volume);
}

/// Mutes/unmutes the Sound.
void Sound::setMute(bool tf)
{
mediaPlayer_->setMuted(tf);
audioOutput_->setMuted(tf);
}

/// Returns the state of the Sound (i.e. playing, stopped, etc...)
QMediaPlayer::State Sound::state()
QMediaPlayer::PlaybackState Sound::state()
{
return mediaPlayer_->state();
return mediaPlayer_->playbackState();
}

/// Returns the volume of the Sound. 0-100.
int Sound::volume()
{
return mediaPlayer_->volume();
return audioOutput_->volume();
}

/// Executed each time the state of the internal MediaPlayer changes.
Expand All @@ -71,7 +73,7 @@ void Sound::stateChanged_()
// - if so, play it again
// - otherwise, emit finished() signal

if (mediaPlayer_->state() == QMediaPlayer::StoppedState){
if (mediaPlayer_->playbackState() == QMediaPlayer::StoppedState){
if (numTimesPlayed_ < numTimesToPlay_ || numTimesToPlay_ == -1){
mediaPlayer_->setPosition(0);
mediaPlayer_->play();
Expand Down
3 changes: 2 additions & 1 deletion qge/Sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Sound: public QObject
void setVolume(int volume);
void setMute(bool tf);

QMediaPlayer::State state();
QMediaPlayer::PlaybackState state();
int volume();

signals:
Expand All @@ -40,6 +40,7 @@ class Sound: public QObject
public slots:
void stateChanged_();
private:
QAudioOutput* audioOutput_;
QMediaPlayer* mediaPlayer_;
int numTimesPlayed_;
int numTimesToPlay_;
Expand Down
1 change: 1 addition & 0 deletions qge/Vendor.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include <QPolygonF>
#include <QMetaType>
#include <QMediaPlayer>
#include <QAudioOutput>
#include <QtGlobal>
#include <QBrush>
#include <QFont>
Expand Down