File indexing completed on 2025-02-09 07:08:06
0001 /* 0002 * SPDX-FileCopyrightText: 2020-2021 Devin Lin <devin@kde.org> 0003 * 0004 * SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "audioplayer.h" 0008 0009 AudioPlayer *AudioPlayer::instance() 0010 { 0011 static AudioPlayer *s_audioPlayer = new AudioPlayer(qApp); 0012 return s_audioPlayer; 0013 } 0014 0015 AudioPlayer::AudioPlayer(QObject *parent) 0016 : QMediaPlayer(parent) 0017 { 0018 m_audioOutput = new QAudioOutput(this); 0019 setAudioOutput(m_audioOutput); 0020 connect(this, &AudioPlayer::playbackStateChanged, this, &AudioPlayer::handleStateChange); 0021 } 0022 0023 void AudioPlayer::handleStateChange(PlaybackState state) 0024 { 0025 if (state == QMediaPlayer::StoppedState) { 0026 wasStopped = true; 0027 } else if (state == QMediaPlayer::PlayingState) { 0028 wasStopped = false; 0029 } 0030 } 0031 0032 void AudioPlayer::setMediaPath(QString path) 0033 { 0034 setSource(QUrl::fromLocalFile(path)); 0035 } 0036 0037 void AudioPlayer::setVolume(float volume) 0038 { 0039 m_audioOutput->setVolume(volume); 0040 }