1

When I use pygame to play mp3s, they play too quickly.

Of course I looked around and found the same problem here. But unfortunately the solution didn't work. I know the mp3s are encoded at 16000 Hz, and I verified this using mutagen. But specifying the frequency didn't help, and in fact changing frequencies didn't change the speed of playback.

I also found this answer, which effectively suggested a different initialization method. But that didn't work for me either.

I'm running a python script as root, and using a USB sound card. I edited my alsa configuration file per this answer to set it as the default audio out for root. Then I'm using this code (or variations per the above SO answers):

pygame.mixer.init(frequency=16000)
pygame.mixer.music.load('myfile.mp3')
pygame.mixer.music.play()

It always plays at what sounds like twice the speed, even when I use different values for the frequency.

~~~~~

Here's the code in more detail:

import boto3
from io import StringIO
import json
import os.path
from contextlib import closing
from time import sleep
import pygame

pygame.mixer.pre_init(16000, -16, 2, 2048)
pygame.init()
#pygame.mixer.init(frequency=16000)

#pygame.mixer.quit()
#pygame.mixer.pre_init(16384, -16, 2, 1024*3)
#pygame.mixer.init()

def play_file(filename):
    pygame.mixer.music.load(filename)
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
        sleep(0.1)

play_file('myfile.mp3')
Twiffy
  • 425
  • 1
  • 4
  • 10
  • This might be just plain silly, but you've confirmed that the original .mp3 file *is not 2x the speed*, right? (That happened to me once for some reason.) Also, could you provide a little more of your program so that it's easier for us to debug? I don't see anything wrong with this right now; try leaving out the `frequency` parameter? – Jerrybibo Jul 03 '18 at 13:24
  • Correct, confirmed! I can play the mp3 via the command line, e.g. `mplayer myfile.mp3`, and it works as expected. Have tried leaving out the frequency parameter. Just added more detailed code. – Twiffy Jul 03 '18 at 19:04

0 Answers0