Warning, file /education/parley/src/practice/audiobutton.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2010 Daniel Laidig <d.laidig@gmx.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "audiobutton.h"
0007 
0008 #include <KLocalizedString>
0009 
0010 using namespace Practice;
0011 
0012 AudioButton::AudioButton(QWidget *parent)
0013     : QToolButton(parent)
0014     , m_player(0)
0015 {
0016     setEnabled(false);
0017     setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
0018     setText(i18n("Play"));
0019     setToolTip(i18n("Play"));
0020     connect(this, &AudioButton::clicked, this, &AudioButton::playAudio);
0021     connect(parent, SIGNAL(stopAudio()), this, SLOT(stopAudio()));
0022 }
0023 
0024 void AudioButton::setSoundFile(const QUrl &soundFile)
0025 {
0026     m_url = soundFile;
0027     setEnabled(!m_url.isEmpty() && m_url.isLocalFile());
0028 }
0029 
0030 void AudioButton::playAudio()
0031 {
0032     if (!m_player) {
0033         m_player = new QMediaPlayer(this);
0034         connect(m_player, &QMediaPlayer::stateChanged, this, &AudioButton::playerStateChanged);
0035     } else {
0036         if (m_player->state() == QMediaPlayer::PlayingState) {
0037             m_player->stop();
0038         }
0039     }
0040     m_player->setMedia(m_url);
0041     m_player->setVolume(50);
0042     m_player->play();
0043 }
0044 
0045 void AudioButton::stopAudio()
0046 {
0047     if (m_player && m_player->state() == QMediaPlayer::PlayingState) {
0048         m_player->stop();
0049     }
0050 }
0051 
0052 void AudioButton::playerStateChanged(QMediaPlayer::State newState)
0053 {
0054     switch (newState) {
0055     case QMediaPlayer::PlayingState:
0056         setIcon(QIcon::fromTheme(QStringLiteral("media-playback-stop")));
0057         setText(i18n("Stop"));
0058         setToolTip(i18n("Stop"));
0059         break;
0060     case QMediaPlayer::StoppedState:
0061     case QMediaPlayer::PausedState:
0062         setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
0063         setToolTip(i18n("Play"));
0064         setText(i18n("Play"));
0065         break;
0066     }
0067 }