I'm trying to use pygame.mixer.music.load
to load an audio file and play it. However, even though the file exists and is playable, I'm still getting a "No such file or directory" error.
Here is the snippet of code:
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'.
>>> import pygame
pygame 2.5.1 (SDL 2.28.2, Python 3.11.4)
Double-checked that the file exists in two different directories:
- C:\Users\creep\sound.mp3
- C:\Users\creep\AppData\Local\Temp\tmpbux7qvnu.mp3
Additional Information
Interestingly, when I run the following code snippet, the audio file plays successfully:
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)