pygame.error: mixer not initialized and pygame.error: WASAPI can’t find requested audio endpoint: element not found.

created at 10-28-2021 views: 84

When using Python to develop games, it is inevitable that the Pygame module will be used. This module has a sound function. Using this function, we can add sound effects to our game.

Problem Description

To use the sound module, we must initialize our game at the beginning of the main function, so we add the following statement at the beginning of the main function to initialize the game.

# Game initialization
pygame.init()

But when I run the program, I found that the game window crashed, and an error message appeared, as follows:

D:\Game\TankWar\venv\Scripts\python.exe D:/Game/TankWar/main.py
pygame 2.0.2 (SDL 2.0.16, Python 3.8.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "D:/Game/TankWar/main.py", line 41, in <module>
    is_quit_game = run_Game(config)
  File "D:/Game/TankWar/main.py", line 22, in run_Game
    sounds[key] = pygame.mixer.Sound(value)
pygame.error: mixer not initialized

Process finished with exit code 1

It even said that I did not initialize the mixer! ! ! No way, let's just follow his error report and initialize the mixer separately for him.

pygame.init()
pygame.mixer.init()

WTF! ! ! The following error message still appears, and the game window still crashes.

D:\Game\TankWar\venv\Scripts\python.exe D:/Game/TankWar/main.py
pygame 2.0.2 (SDL 2.0.16, Python 3.8.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "D:/Game/TankWar/main.py", line 42, in <module>
    is_quit_game = run_Game(config)
  File "D:/Game/TankWar/main.py", line 17, in run_Game
    pygame.mixer.init()
pygame.error: WASAPI can't find requested audio endpoint: The element was not found.

Process finished with exit code 1

solution

After repeated tests, I found that it can run normally sometimes, and sometimes the above error will appear. Finally, I found an article by a big brother, which solved this problem.

The problem with the headset! ! !
Because I am using a desktop computer and no audio is connected, there has been no audio output device. This caused pygame to not know where to output the sound (in this case, the audio device could not be found), which caused an error. After plugging in the audio device (i.e. my earphones), it was... solved. . .

created at:10-28-2021
edited at: 10-28-2021: