Unable to play music file in pygame

Asked 1 years ago, Updated 1 years ago, 67 views

Pygame.mixer is unable to play the downloaded music file on the web.

importos
import pygame
pygame.init()
sound=pygame.mixer.Sound("C:/Users/syosh/Desktop/pyworks/data/ball_collided.wav")
sound=pygame.mixer.Sound("C:/Users/syosh/Desktop/pyworks/data/coin07.mp3")

Both ball_collided.wav and coin07.mp3 are stored in the same folder, but the following error message appears:

error message

 pygame.error: Unable to open file'C:/Users/syosh/Desktop/pyworks/data/coin07.mp3

Game sound free material link
https://taira-komori.jpn.org/game01.html

python mp3 pygame

2022-09-30 14:51

1 Answers

pygame.mixer.Sound description.
Supported file format issues.
It seems that we can only handle the following two things.

The Sound can be loaded from an OGG audio file or from an uncompressed WAV.

Sound can be loaded from an OGG audio file or uncompressed WAV.

@metropolis, you'll need to either convert it in advance, or read it in some other library that supports the format, convert it to .wav on memory, and pass it to pygame.

Supplement:

According to the article below, python music supports format conversion from .mp3 only pydub+ffmpeg, and it seems that it is not only a conversion but also a combination of writing to a file, so it doesn't seem to have the ability to convert only on memory.
Playing and Recording Sound in Python
See the Comparison of Audio Libraries section

There seems to be many combinations of soundfile+libsndfile, but it clearly states that .mp3 is not supported.

I have decided that I will not be adding support for MPEG Layer 3 (commonly known as MP3) due to the patent issues surrounding this file format.See the FAQ for more.

There seems to be a way to combine pydub+ffmpeg and numpy as follows, but it must be troublesome.
[Python/pydub] Convert mp3 and wav data to a NumPy array
miniaudio

Python bindings for the miniaudio library and its decoders (mp3, flac, ogg vorbis, wav)

Python bindings for miniaudio libraries and their decoders (mp3, flac, ogg vorbis, wav).


2022-09-30 14:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.