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

使用pygame.mixer.music.load加载存在文件却提示“找不到文件或目录”

pygame.mixer.music.load加载临时MP3文件报“No such file or directory”错误

我尝试使用pygame.mixer.music.load加载音频文件并播放,但即便文件存在且可正常播放,仍收到“No such file or directory”错误。

代码片段

import os
import pygame
import tempfile
from pydub import AudioSegment

tmp_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
bgm = AudioSegment.from_mp3(r"C:\Users\creep\sound.mp3")
bgm = bgm + 10
bgm.export(tmp_file.name, format='mp3')

print(f"{os.path.exists(tmp_file.name)=}")

pygame.mixer.init()
pygame.mixer.music.load(tmp_file.name)
pygame.mixer.music.play()

运行报错信息

> python .\test_so.py
pygame 2.5.1 (SDL 2.28.2, Python 3.11.4)
Hello from the pygame community. https://www.pygame.org/contribute.html
os.path.exists(tmp_file.name)=True
Traceback (most recent call last):
  File "C:\Users\creep\work\test_so.py", line 15, in <module>
    pygame.mixer.music.load(tmp_file.name)
pygame.error: No such file or directory: 'C:\Users\creep\AppData\Local\Temp\tmpbux7qvnu.mp3'.

已确认信息

  • C:\Users\creep\sound.mp3 文件存在且可访问
  • C:\Users\creep\AppData\Local\Temp\tmpbux7qvnu.mp3 文件存在且可访问

额外测试结果

单独运行以下代码片段时,该临时音频文件可成功播放:

import pygame
import time

pygame.mixer.init()
pygame.mixer.music.load(r"C:\Users\creep\AppData\Local\Temp\tmpbux7qvnu.mp3")
pygame.mixer.music.play()
time.sleep(5)

内容的提问来源于stack exchange,提问作者Creeper Saviour

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 02:33:13