some updates
This commit is contained in:
@@ -5,6 +5,7 @@ import comtypes
|
||||
import ctypes
|
||||
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
|
||||
from ctypes import cast, POINTER
|
||||
from comtypes import CLSCTX_ALL
|
||||
from typing import Dict, Any
|
||||
|
||||
from winrt.windows.media.control import GlobalSystemMediaTransportControlsSessionManager as MediaManager
|
||||
@@ -15,10 +16,25 @@ parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
||||
if parent_dir not in sys.path:
|
||||
sys.path.append(parent_dir)
|
||||
|
||||
VOLUME = 0
|
||||
|
||||
|
||||
|
||||
async def get_volume(args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
global VOLUME
|
||||
|
||||
# Get default audio device
|
||||
devices = AudioUtilities.GetSpeakers()
|
||||
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
|
||||
volume = cast(interface, POINTER(IAudioEndpointVolume))
|
||||
|
||||
# Get current volume level (returns a float between 0.0 and 1.0)
|
||||
current_volume = volume.GetMasterVolumeLevelScalar()
|
||||
|
||||
return {"value": round(current_volume*100)}
|
||||
|
||||
async def set_volume(args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
global VOLUME
|
||||
"""
|
||||
Sets the Windows system volume using the Core Audio API.
|
||||
|
||||
@@ -36,12 +52,13 @@ async def set_volume(args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
comtypes.CoInitialize()
|
||||
|
||||
# Get volume level from args
|
||||
level = args.get('level', 0.5) # Default to 50% if not specified
|
||||
level = args.get('level', 50) # Default to 50% if not specified
|
||||
|
||||
# Validate input level
|
||||
if not isinstance(level, (int, float)) or not 0.0 <= level <= 1.0:
|
||||
raise ValueError("Volume level must be a float between 0.0 and 1.0")
|
||||
|
||||
if not isinstance(level, (int, float)) or not 0 <= level <= 100:
|
||||
raise ValueError("Volume level must be a float between 0 and 100")
|
||||
VOLUME = level
|
||||
level = level/100
|
||||
# Get audio endpoint interface
|
||||
devices = AudioUtilities.GetSpeakers()
|
||||
interface = devices.Activate(IAudioEndpointVolume._iid_, comtypes.CLSCTX_ALL, None)
|
||||
@@ -106,4 +123,16 @@ async def prev(args):
|
||||
# назад
|
||||
await current_session.try_skip_previous_async()
|
||||
else:
|
||||
print("No media session is active.")
|
||||
print("No media session is active.")
|
||||
|
||||
async def play_pause(args):
|
||||
# Получаем менеджер медиасессий
|
||||
sessions = await MediaManager.request_async()
|
||||
current_session = sessions.get_current_session()
|
||||
|
||||
if current_session:
|
||||
await current_session.try_toggle_play_pause_async()
|
||||
else:
|
||||
print("No media session is active.")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user