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"
|
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.
|
"""Play a sound using a new VLC subprocess instance.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
sound: The Sound object to play
|
sound: The Sound object to play
|
||||||
|
volume: Volume level (0-100). If None, uses current player volume.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: True if VLC process was launched successfully, False otherwise
|
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)
|
logger.error("Sound file not found: %s", sound_path)
|
||||||
return False
|
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
|
# VLC command arguments for immediate playback
|
||||||
cmd = [
|
cmd = [
|
||||||
self.vlc_executable,
|
self.vlc_executable,
|
||||||
@@ -97,6 +111,7 @@ class VLCPlayerService:
|
|||||||
"--no-video", # Audio only
|
"--no-video", # Audio only
|
||||||
"--no-repeat", # Don't repeat
|
"--no-repeat", # Don't repeat
|
||||||
"--no-loop", # Don't loop
|
"--no-loop", # Don't loop
|
||||||
|
f"--volume={volume}", # Set volume to match player
|
||||||
]
|
]
|
||||||
|
|
||||||
# Launch VLC process asynchronously without waiting
|
# Launch VLC process asynchronously without waiting
|
||||||
@@ -144,7 +159,7 @@ class VLCPlayerService:
|
|||||||
stderr=asyncio.subprocess.PIPE,
|
stderr=asyncio.subprocess.PIPE,
|
||||||
)
|
)
|
||||||
|
|
||||||
stdout, stderr = await find_process.communicate()
|
stdout, _stderr = await find_process.communicate()
|
||||||
|
|
||||||
if find_process.returncode != 0:
|
if find_process.returncode != 0:
|
||||||
# No VLC processes found
|
# No VLC processes found
|
||||||
@@ -369,8 +384,19 @@ class VLCPlayerService:
|
|||||||
),
|
),
|
||||||
) from e
|
) from e
|
||||||
|
|
||||||
# Play the sound using VLC
|
# Get current player volume
|
||||||
success = await self.play_sound(sound)
|
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
|
# Deduct credits based on success
|
||||||
await credit_service.deduct_credits(
|
await credit_service.deduct_credits(
|
||||||
|
|||||||
Reference in New Issue
Block a user