feat: Enhance play_sound method to accept volume parameter and retrieve current volume
This commit is contained in:
@@ -70,11 +70,12 @@ class VLCPlayerService:
|
||||
)
|
||||
return "vlc"
|
||||
|
||||
async def play_sound(self, sound: Sound) -> bool:
|
||||
async def play_sound(self, sound: Sound, volume: int | None = None) -> bool:
|
||||
"""Play a sound using a new VLC subprocess instance.
|
||||
|
||||
Args:
|
||||
sound: The Sound object to play
|
||||
volume: Volume level (0-100). If None, uses current player volume.
|
||||
|
||||
Returns:
|
||||
bool: True if VLC process was launched successfully, False otherwise
|
||||
@@ -87,6 +88,19 @@ class VLCPlayerService:
|
||||
logger.error("Sound file not found: %s", sound_path)
|
||||
return False
|
||||
|
||||
# Get volume from player service if not provided
|
||||
if volume is None:
|
||||
try:
|
||||
from app.services.player import get_player_service # noqa: PLC0415
|
||||
player = get_player_service()
|
||||
volume = player.state.volume
|
||||
except RuntimeError:
|
||||
logger.warning("Could not get player volume, using default 80")
|
||||
volume = 80
|
||||
|
||||
# Ensure volume is in valid range and is an integer
|
||||
volume = max(0, min(100, int(volume))) if volume is not None else 80
|
||||
|
||||
# VLC command arguments for immediate playback
|
||||
cmd = [
|
||||
self.vlc_executable,
|
||||
@@ -97,6 +111,7 @@ class VLCPlayerService:
|
||||
"--no-video", # Audio only
|
||||
"--no-repeat", # Don't repeat
|
||||
"--no-loop", # Don't loop
|
||||
f"--volume={volume}", # Set volume to match player
|
||||
]
|
||||
|
||||
# Launch VLC process asynchronously without waiting
|
||||
@@ -144,7 +159,7 @@ class VLCPlayerService:
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
|
||||
stdout, stderr = await find_process.communicate()
|
||||
stdout, _stderr = await find_process.communicate()
|
||||
|
||||
if find_process.returncode != 0:
|
||||
# No VLC processes found
|
||||
@@ -369,8 +384,19 @@ class VLCPlayerService:
|
||||
),
|
||||
) from e
|
||||
|
||||
# Play the sound using VLC
|
||||
success = await self.play_sound(sound)
|
||||
# Get current player volume
|
||||
try:
|
||||
from app.services.player import get_player_service # noqa: PLC0415
|
||||
player = get_player_service()
|
||||
current_volume = player.state.volume
|
||||
except RuntimeError:
|
||||
logger.warning(
|
||||
"Could not get player volume for credit play, using default 80",
|
||||
)
|
||||
current_volume = 80
|
||||
|
||||
# Play the sound using VLC with current player volume
|
||||
success = await self.play_sound(sound, current_volume)
|
||||
|
||||
# Deduct credits based on success
|
||||
await credit_service.deduct_credits(
|
||||
|
||||
Reference in New Issue
Block a user