Skip to content

Commit

Permalink
Volume slider changes.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vorlie committed Dec 19, 2024
1 parent 296d442 commit ca2a929
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
40 changes: 38 additions & 2 deletions core/musicPlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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."""
Expand Down
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
altgraph==0.17.4
comtypes==1.4.8
contourpy==1.2.1
cycler==0.12.1
darkdetect==0.7.1
Expand All @@ -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
Expand All @@ -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

0 comments on commit ca2a929

Please sign in to comment.