Skip to content

Instantly share code, notes, and snippets.

@Pavel-Durov
Created December 1, 2023 17:40
Show Gist options
  • Save Pavel-Durov/cc1af78c6a8ebbacef7773b9a06cd328 to your computer and use it in GitHub Desktop.
Save Pavel-Durov/cc1af78c6a8ebbacef7773b9a06cd328 to your computer and use it in GitHub Desktop.
Play mp3 sound on raspberry pi
# pip install pygame
```python
import pygame
def play_mp3(file_path):
pygame.mixer.init()
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
# You may need to add a delay to give the file some time to start playing
pygame.time.wait(1000)
# Wait until the music is finished playing
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
pygame.mixer.quit()
if __name__ == "__main__":
mp3_file_path = "path/to/your/file.mp3"
play_mp3(mp3_file_path)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment