2022-06-15

How to play base64-encoded sound in Python [duplicate]

I want to play a base64-encoded sound in Python, I've tried using Pygame.mixer but all I get is a hiss of white noise.

This is an example of my code:

import pygame

coinflip = b'data:audio/ogg;base64,T2dnUwACAAAAAAAAAACYZ...'  # Truncated for brevity
flip  = pygame.mixer.Sound(coinflip)
ch = flip.play()
while ch.get_busy():
    pygame.time.wait(100)

The pygame mixer works well if I import a wav/mp3/ogg file, but I want to write a compact self-contained program that doesn't need external files, so I'm trying to embed a base64 encoded version of the sound in the Python code.

NB: The solution doesn't need to be using pygame, but it would be preferable since I'm already using it elsewhere in the program.



1 comment:



  1. Very Informative and creative contents. This concept is a good way to enhance the knowledge. thanks for sharing.
    Continue to share your knowledge through articles like these, and keep posting more blogs.
    And more Information Flutter App Development Services in USA

    ReplyDelete