繁体   English   中英

pycharm工作在Mac上的pygame中无法获取声音或音乐

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

我无法让pygame处理音乐或声音,我已经尝试过

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()

但这会导致错误:

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

并且返回值为

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

我已经尝试了很多事情,但是我认为正是文件给了我这些错误。

有了这个确切的代码:

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()

我能够播放文件ts.wav (与Python文件位于同一文件夹中),并且可以正常运行。

我似乎唯一要做的就是添加pygame.mixer.pre_init(44100, 16, 2, 4096) ,然后移动声音的位置。

如果还是不行,我觉得PyCharm 可能是罪魁祸首。 我过去用过它,这很奇怪。

我有同样的问题。 我解决问题的方法是将原始.wav声音文件转换为.ogg(使用VLC;在线转换器也可以工作)。

并不是说.wav文件通常是不好的:我尝试了一个不同的.wav文件,它工作得很好。 但是我的.wav文件不正确/损坏了,并且从我用来制作文件的记录设备中取出来。 无论如何,转换解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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