Quantcast
Viewing all articles
Browse latest Browse all 5458

Python • Re: No sound using Pygame

No mention of how you are selecting analogue audio!

I found the following, tested on Bookworm 64bit with a Pi4. May need to use 'alsamixer' from a terminal window to increase volume.

Basically finding the audio devices and selecting the first which happens to be analogue audio.

Code:

from time import sleepimport pygameimport pygame._sdl2.audio as sdl2_audiodef get_devices(capture_devices: bool = False) -> tuple[str, ...]:    init_by_me = not pygame.mixer.get_init()    if init_by_me:        pygame.mixer.init()    devices = tuple(sdl2_audio.get_audio_device_names(capture_devices))    if init_by_me:        pygame.mixer.quit()    return devicesdef play(file_path: str, device:str = None):    if device is None:        devices = get_devices()        print(devices)        if not devices:            raise RuntimeError("No device!")        device = devices[0]    print("Play: {}\r\nDevice: {}".format(file_path, device))    pygame.mixer.init(devicename=device)    pygame.mixer.music.load(file_path)    pygame.mixer.music.play()    print(pygame.mixer.music.get_volume())    try:        while True:            sleep(0.1)    except KeyboardInterrupt:        pass    pygame.mixer.quit()    play('/home/pi/file_example_MP3_700KB.mp3')

Statistics: Posted by rpiMike — Wed Mar 13, 2024 3:21 pm



Viewing all articles
Browse latest Browse all 5458

Trending Articles