Created
December 1, 2023 17:40
-
-
Save Pavel-Durov/cc1af78c6a8ebbacef7773b9a06cd328 to your computer and use it in GitHub Desktop.
Play mp3 sound on raspberry pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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