add_music_functions #6

Merged
Dmitrium12 merged 3 commits from add_music_functions into master 2024-05-09 16:36:27 +07:00
Showing only changes of commit aa1e424e8b - Show all commits
+16 -26
View File
@@ -12,12 +12,6 @@ class MediaPlayerController:
elif self.os_type == 'Linux': elif self.os_type == 'Linux':
self._linux_control("play-pause") self._linux_control("play-pause")
def stop(self):
if self.os_type == 'Windows':
self._windows_control("stop")
elif self.os_type == 'Linux':
self._linux_control("stop")
def next_track(self): def next_track(self):
if self.os_type == 'Windows': if self.os_type == 'Windows':
self._windows_control("next") self._windows_control("next")
@@ -30,28 +24,24 @@ class MediaPlayerController:
elif self.os_type == 'Linux': elif self.os_type == 'Linux':
self._linux_control("previous") self._linux_control("previous")
@staticmethod def _windows_play_pause(self):
def _windows_play_pause(): import win32con
from pycaw.pycaw import AudioUtilities self.key_press(win32con.VK_MEDIA_PLAY_PAUSE)
sessions = AudioUtilities.GetAllSessions()
for session in sessions: def _windows_control(self, action):
if session.Process and session.AudioSessionControl: import win32con
control = session.AudioSessionControl if action == "next":
if control and control.State == 1: self.key_press(win32con.VK_MEDIA_NEXT_TRACK)
control.Stop() elif action == "previous":
else: self.key_press(win32con.VK_MEDIA_PREV_TRACK)
control.Play()
@staticmethod @staticmethod
def _windows_control(action): def key_press(key_code):
from pycaw.pycaw import AudioUtilities, IAudioSessionControl2 """Симуляция нажатия и отпускания клавиши."""
sessions = AudioUtilities.GetAllSessions() import win32api
for session in sessions: import win32con
if session.Process and isinstance(session.ControlInterface, IAudioSessionControl2): win32api.keybd_event(key_code, 0, 0, 0)
if action == "stop": win32api.keybd_event(key_code, 0, win32con.KEYEVENTF_KEYUP, 0)
session.SimpleAudioVolume.SetMute(1, None)
elif action == "next" or action == "previous":
pass
@staticmethod @staticmethod
def _linux_control(command): def _linux_control(command):