pygame.mixer 模組提供簡單易用的音頻播放功能,支援 MP3、OGG、WAV 等格式。本文從基本播放、音量控制、播放列表,到音效同時播放、淡入淡出等進階技巧一次講清楚。
pip install pygame
# 部分 Linux 發布版可能需額外安裝系統庫
# Ubuntu/Debian: sudo apt-get install libsdl2-mixer-dev libmpg123-dev
| 類別 | 模組 | 適用場景 | 特性 |
|---|---|---|---|
| 背景音樂 | pygame.mixer.music | 長音軌、BGM、播放列表 | 串流播放、佔用單一聲道、支援佇列 |
| 短音效 | pygame.mixer.Sound | 按鈕音、碰撞音、UI 回饋 | 載入記憶體、可同時多聲道播放、延遲極低 |
import pygame
import time
pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=512)
pygame.mixer.music.load('bgm.ogg')
pygame.mixer.music.set_volume(0.6) # 0.0 ~ 1.0
pygame.mixer.music.play(loops=-1) # -1 表示無限循環
print('播放中... 按 Enter 停止')
input()
pygame.mixer.music.fadeout(2000) # 2 秒淡出
time.sleep(2)
pygame.quit()
import pygame
pygame.mixer.init()
# 預載音效至記憶體
jump_snd = pygame.mixer.Sound('jump.wav')
coin_snd = pygame.mixer.Sound('coin.ogg')
explosion_snd = pygame.mixer.Sound('explosion.wav')
# 播放 (非阻塞)
jump_snd.play()
coin_snd.play()
# 可指定聲道
channel = explosion_snd.play()
if channel:
channel.set_volume(0.8)
# 等待所有音效播完 (簡易示範)
pygame.time.wait(2000)
pygame.quit()
# 音樂模組
pygame.mixer.music.pause() # 暫停
pygame.mixer.music.unpause() # 繼續
pygame.mixer.music.stop() # 停止
pygame.mixer.music.rewind() # 回到開頭
pygame.mixer.music.set_pos(30.5) # 跳轉至 30.5 秒 (僅支援部分格式)
pygame.mixer.music.get_busy() # 是否正在播放
pygame.mixer.music.queue('next.ogg')# 排隊下一首 (播放完畢自動接續)
# Sound 物件
snd = pygame.mixer.Sound('effect.wav')
snd.play(loops=0, maxtime=0, fade_ms=0)
snd.stop()
snd.set_volume(0.5)
snd.get_length() # 秒數
snd.get_num_channels() # 正在播放的聲道數
# 全域聲道管理
pygame.mixer.set_num_channels(32) # 預設 8,複雜遊戲建議調大
pygame.mixer.get_busy() # 任一聲道是否忙碌
pygame.mixer.fadeout(1000) # 所有聲道淡出
class AudioManager:
"""簡易音效/音樂管理器,支援分類音量控制"""
def __init__(self):
pygame.mixer.init()
pygame.mixer.set_num_channels(32)
self.sounds = {}
self.music_volume = 0.7
self.sfx_volume = 1.0
self.current_music = None
def load_sfx(self, name, path):
self.sounds[name] = pygame.mixer.Sound(path)
def play_sfx(self, name, volume=None):
snd = self.sounds.get(name)
if snd:
ch = snd.play()
if ch and volume is not None:
ch.set_volume(volume * self.sfx_volume)
elif ch:
ch.set_volume(self.sfx_volume)
def play_music(self, path, loops=-1, fade_ms=0):
pygame.mixer.music.load(path)
pygame.mixer.music.set_volume(self.music_volume)
pygame.mixer.music.play(loops, fade_ms=fade_ms)
self.current_music = path
def set_music_volume(self, vol):
self.music_volume = max(0.0, min(1.0, vol))
pygame.mixer.music.set_volume(self.music_volume)
def set_sfx_volume(self, vol):
self.sfx_volume = max(0.0, min(1.0, vol))
# 使用範例
audio = AudioManager()
audio.load_sfx('jump', 'assets/jump.wav')
audio.load_sfx('hit', 'assets/hit.ogg')
audio.play_music('assets/bgm.ogg')
audio.play_sfx('jump')
libmpg123,某些平台預設未編入。改用 OGG (Vorbis) 或 WAV 格式最穩定。buffer 參數 (如 256/512),或改用 Sound 而非 music 播放短音效。set_num_channels(32) 或更高。pygame.quit() 前加 pygame.time.wait(ms) 或輪詢 get_busy()。若需更豐富功能 (多軌混音、音訊串流、3D 定位、濾波器),可考慮:
python-vlc (libvlc 綁定,格式支援最全)pydub + simpleaudio (純 Python 處理 + 播放)pyo / pedalboard (專業音訊 DSP)