You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已使用Pygame循环,如何不启动Pyglet循环独立播放音频?

Using Pyglet Audio Without Its Main Loop (Alongside Pygame)

Got it, let's solve this! You want Pyglet's precise volume control but don't want to ditch your Pygame loop—totally possible. The key is to use Pyglet's Player class and manually update it in your existing Pygame loop, instead of relying on pyglet.app.run().

Step-by-Step Solution

  1. Set up both libraries
    Import Pygame and the necessary Pyglet media modules. Initialize Pygame like you normally would, then load your audio with Pyglet.

  2. Manage Pyglet's Player manually
    Pyglet's Player only needs regular updates to handle audio buffering and playback progress. You can call its update(dt) method in your Pygame loop, where dt is the time elapsed since the last frame (in seconds).

  3. Precise Volume Control
    Unlike Pygame's often limited volume steps, Pyglet's player.volume accepts any float between 0.0 (mute) and 1.0 (max volume)—perfect for fine-grained control.

Full Code Example

import pygame
import pyglet
from pyglet.media import Player, load

# Initialize Pygame as usual
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

# Load audio with Pyglet
audio_source = load("your_audio_file.wav")  # Supports WAV, MP3, OGG, etc.
audio_player = Player()
audio_player.queue(audio_source)
audio_player.volume = 0.6  # Start with 60% volume (precision here is great!)
audio_player.play()

# Your existing Pygame main loop
running = True
while running:
    # Calculate time since last frame (for Pyglet's update)
    delta_time = clock.tick(60) / 1000.0  # Convert milliseconds to seconds

    # Handle Pygame events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        # Example: Adjust volume with arrow keys
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                audio_player.volume = min(audio_player.volume + 0.02, 1.0)
                print(f"Current volume: {audio_player.volume:.3f}")
            if event.key == pygame.K_DOWN:
                audio_player.volume = max(audio_player.volume - 0.02, 0.0)
                print(f"Current volume: {audio_player.volume:.3f}")

    # Manually update Pyglet's audio player
    audio_player.update(delta_time)

    # Your Pygame rendering logic here
    screen.fill((20, 20, 20))
    pygame.display.flip()

# Cleanup when done
audio_player.pause()
pygame.quit()

Key Notes

  • Multiple Audio Files: If you need to play multiple sounds, create separate Player instances for each, and call update(dt) on all of them in your loop.
  • Audio Formats: Pyglet supports most common formats out of the box, but if you run into issues with MP3/OGG, ensure you have the required decoders installed (they're usually included with standard Pyglet installs).
  • No Blocking: Since we're avoiding pyglet.app.run(), there's no blocking—your Pygame loop runs as normal, and audio playback stays in sync.

内容的提问来源于stack exchange,提问作者Rebagliati Loris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:08:07