diff options
Diffstat (limited to 'AudioPlayer.py')
| -rw-r--r-- | AudioPlayer.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/AudioPlayer.py b/AudioPlayer.py new file mode 100644 index 0000000..8bf66e6 --- /dev/null +++ b/AudioPlayer.py @@ -0,0 +1,68 @@ +# Soundboard Engine - customizable soundboard +# Copyright (C) 2026 Vidhu Kant Sharma <vidhukant@vidhukant.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +from PySide6.QtCore import QObject, QUrl, Signal +from PySide6.QtMultimedia import QAudioOutput, QMediaPlayer + +from Soundboard import SoundboardSound + + +class PlayerStatus: + def __init__(self, sound, status): + self.sound = sound + self.status = status + + +class AudioPlayer(QObject): + status_updated = Signal(PlayerStatus) + + def __init__(self, parent=None): + super().__init__(parent) + + self.status = PlayerStatus(SoundboardSound("", ""), "STOPPED") + self.player = QMediaPlayer(self) + self.audio_output = QAudioOutput(self) + self.player.setAudioOutput(self.audio_output) + + self.player.mediaStatusChanged.connect(self._on_media_status_changed) + self.player.playbackStateChanged.connect(self._on_state_changed) + + def play_sound(self, sound): + if not sound or not sound.path: + return + + self.status.sound = sound + self.player.setSource(QUrl.fromLocalFile(self.status.sound.path)) + self.audio_output.setVolume(1.0) + self.status.status = "PLAYING" + self.player.play() + + def stop_sound(self): + self.player.stop() + + def _on_media_status_changed(self, status): + if status == QMediaPlayer.MediaStatus.EndOfMedia: + self.status.status = "STOPPED" + + self.status_updated.emit(self.status) + + def _on_state_changed(self, state): + if state == QMediaPlayer.PlaybackState.PlayingState: + self.status.status = "PLAYING" + elif state == QMediaPlayer.PlaybackState.StoppedState: + self.status.status = "STOPPED" + + self.status_updated.emit(self.status) |