You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.3 KiB
C++

1 year ago
#include "qw_soundplayer.h"
QWSoundPlayer::QWSoundPlayer(QObject *parent) :
QObject(parent),
i_volume(100),
b_muted(false),
player_loop(new QMediaPlayer(this)),
playlist_loop(new QMediaPlaylist(this)),
player_single(new QMediaPlayer(this)),
player_music(new QMediaPlayer(this)),
playlist_music(new QMediaPlaylist(this))
{
// Music
playlist_music->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
playlist_music->setCurrentIndex(0);
player_music->setAudioRole(QAudio::MusicRole);
player_music->setPlaylist(playlist_music);
QObject::connect(this,
SIGNAL(transferSetMuteness(bool)),
player_music,
SLOT(setMuted(bool)));
QObject::connect(this,
SIGNAL(transferAdjustVolume(int)),
player_music,
SLOT(setVolume(int)));
QObject::connect(this,
SIGNAL(playMusic()),
player_music,
SLOT(play()));
// Loop sounds
playlist_loop->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
playlist_loop->setCurrentIndex(0);
player_loop->setAudioRole(QAudio::GameRole);
player_loop->setPlaylist(playlist_loop);
QObject::connect(this,
SIGNAL(transferSetMuteness(bool)),
player_loop,
SLOT(setMuted(bool)));
QObject::connect(this,
SIGNAL(transferAdjustVolume(int)),
player_loop,
SLOT(setVolume(int)));
QObject::connect(this,
SIGNAL(playLoop()),
player_loop,
SLOT(play()));
// Single sounds
player_single->setAudioRole(QAudio::GameRole);
QObject::connect(this,
SIGNAL(transferSetMuteness(bool)),
player_single,
SLOT(setMuted(bool)));
QObject::connect(this,
SIGNAL(transferAdjustVolume(int)),
player_single,
SLOT(setVolume(int)));
QObject::connect(this,
SIGNAL(playSingle()),
player_single,
SLOT(play()));
}
int QWSoundPlayer::addMusic(const QString &path)
{
playlist_music->addMedia(QUrl::fromLocalFile(path));
playlist_music->setCurrentIndex(playlist_music->currentIndex() + 1);
return playlist_music->currentIndex() - 1;
}
bool QWSoundPlayer::isMuted() const noexcept
{
return b_muted;
}
int QWSoundPlayer::volume() const noexcept
{
return i_volume;
}
void QWSoundPlayer::playSound(QMediaContent *sound)
{
player_single->stop();
player_single->setMedia(*sound);
emit playSingle();
}
void QWSoundPlayer::playMusic(const int index)
{
playlist_music->setCurrentIndex(index);
emit playMusic();
}
void QWSoundPlayer::stopMusic()
{
player_music->stop();
}
////////////////////////
void QWSoundPlayer::setMuteness(bool mute) noexcept
{
b_muted = mute;
emit transferSetMuteness(b_muted);
}
void QWSoundPlayer::adjustVolume(int vol) noexcept
{
i_volume = vol;
emit transferAdjustVolume(i_volume);
}
void QWSoundPlayer::onEndLevel() noexcept
{
playlist_music->clear();
playlist_music->setCurrentIndex(0);
}