简体   繁体   中英

Can't get sound or music in pygame on mac in pycharm working

I can't get pygame to work with music or sounds, I have tried this

import pygame
pygame.init()
pygame.mixer.init()
song = pygame.mixer.Sound('/Users/Me/PycharmProjects/Porting Tester/MusicTest/Hi.mp3')
display = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Hi")
clock = pygame.time.Clock()
song.play()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
            exit()
    pygame.display.update()
    clock.tick(60)
pygame.quit()

but that causes an error saying:

Python(18309,0x7fff73a5f000) malloc: *** error for object 0x1004dae80: pointer being freed was not allocated ***
set a breakpoint in malloc_error_break to debug

And a return value of

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

I have tried many things but I think it's the file that is giving me these errors.

With this exact code :

import pygame
import os
pygame.init()
pygame.mixer.pre_init(44100, 16, 2, 4096) # Frequency, channel size, channels, buffersize
pygame.mixer.init()

song = pygame.mixer.Sound('ts.wav')

display = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Hi")
clock = pygame.time.Clock()

song.play()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
            exit()
    pygame.display.update()
    clock.tick(60)
pygame.quit()

I was able to play a file, ts.wav (located in the same folder as the Python file), and it functioned correctly.

The only thing I seem to have done is add pygame.mixer.pre_init(44100, 16, 2, 4096) , and move the location of the sound.

If it still doesn't work, I think PyCharm may be to blame. I've used it in the past, and it was weird.

I had the same problem. The way I solved it was converting the original .wav sound files to .ogg (with VLC; online converters work, too).

Not that .wav files are generally bad: I tried a different .wav file and it worked just fine. But my .wav files were somehow bad/corrupted and came out weird from the recording device I had used to make them. Anyway, converting solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM