From ca2a929ec2d7098561f066f12920fea603d5b34b Mon Sep 17 00:00:00 2001 From: vorlie Date: Thu, 19 Dec 2024 03:57:41 +0100 Subject: [PATCH] Volume slider changes. Make the volume slider manage system-level application audio instead of using pygame.mixer volume, however it is now used as a backup when the application is ran as a script. --- core/musicPlayer.py | 40 ++++++++++++++++++++++++++++++++++++++-- requirements.txt | 5 ++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/core/musicPlayer.py b/core/musicPlayer.py index be70cba..52f9201 100644 --- a/core/musicPlayer.py +++ b/core/musicPlayer.py @@ -7,12 +7,15 @@ from PyQt5.QtGui import QIcon, QPixmap, QPainter, QPainterPath from PyQt5.QtCore import QTimer, Qt from pygame import mixer +from PyQt5.QtMultimedia import QMediaPlayer from pynput import keyboard from mutagen.mp3 import MP3 from core.discordIntegration import DiscordIntegration from core.playlistMaker import PlaylistMaker, PlaylistManager from core.settingManager import SettingsDialog from core.logger import setup_logging +from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume +from comtypes import CLSCTX_ALL class MusicPlayer(QMainWindow): def __init__(self, settings, icon_path, config_path, theme, normal): @@ -86,12 +89,31 @@ def __init__(self, settings, icon_path, config_path, theme, normal): self.connection_check_timer.timeout.connect(self.check_discord_connection) self.connection_check_timer.start(10000) # Check every 10 seconds + devices = AudioUtilities.GetSpeakers() + interface = devices.Activate( + IAudioEndpointVolume._iid_, CLSCTX_ALL, None) + self.volume_interface = interface.QueryInterface(IAudioEndpointVolume) + logging.info(f"Initialized IAudioEndpointVolume interface: {self.volume_interface}") + mixer.init() mixer.music.set_endevent(1) self.on_start() self.has_started = False self.is_paused = False + self.audio_session = self.get_audio_session() + + self.volume_update_timer = QTimer() + self.volume_update_timer.timeout.connect(self.update_volume_slider) + self.volume_update_timer.start(100) + + def get_audio_session(self): + sessions = AudioUtilities.GetAllSessions() + for session in sessions: + if session.Process and session.Process.name() == "IotaPlayer.exe": + return session.SimpleAudioVolume + return None + def set_stylesheet(self, theme, normal): if theme == 'dark': stylesheet = f""" @@ -515,9 +537,23 @@ def update_discord_status(self, is_connected): def adjust_volume(self, value): """Adjusts the volume of the music player.""" volume = value / 100.0 - mixer.music.set_volume(volume) + if self.audio_session: + self.audio_session.SetMasterVolume(volume, None) + else: + mixer.music.set_volume(volume) self.volume_label.setText(f"Volume: {value}%") - #logging.info(f"Volume set to: {volume}") + #logging.info(f"Volume set to: {value}%") + + def update_volume_slider(self): + """Updates the volume slider based on the current system volume.""" + if self.audio_session: + current_volume = self.audio_session.GetMasterVolume() * 100 + else: + current_volume = mixer.music.get_volume() * 100 + self.volume_slider.blockSignals(True) + self.volume_slider.setValue(int(current_volume)) + self.volume_slider.blockSignals(False) + self.volume_label.setText(f"Volume: {int(current_volume)}%") def keyPressEvent(self, event): """Handle key press events within the PyQt5 application.""" diff --git a/requirements.txt b/requirements.txt index e21b4da..54c9144 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ altgraph==0.17.4 +comtypes==1.4.8 contourpy==1.2.1 cycler==0.12.1 darkdetect==0.7.1 @@ -10,6 +11,8 @@ numpy==2.0.1 packaging==24.1 pefile==2023.2.7 pillow==10.4.0 +psutil==6.1.0 +pycaw==20240210 pygame==2.6.0 pyinstaller==6.6.0 pyinstaller-hooks-contrib==2024.8 @@ -23,4 +26,4 @@ PyQtDarkTheme @ git+https://github.com/vorlie/PyQtDarkTheme.git@9746d1214bb5c096 python-dateutil==2.9.0.post0 pywin32-ctypes==0.2.2 setuptools==72.1.0 -six==1.16.0 +six==1.16.0 \ No newline at end of file