简体   繁体   English

使用pygame.mixer加载音频

[英]Loading audio using pygame.mixer

I used a previous stack overflow post to help me develop the following code: 我使用了以前的堆栈溢出文章来帮助我开发以下代码:

import os
import pygame
from pygame import *
import sys

GAME_FOLDER = os.path.dirname(__file__)
IMG_FOLDER = os.path.join(GAME_FOLDER, "img")


from time import sleep
pygame.mixer.init()
pygame.mixer.music.load(os.path.join(IMG_FOLDER, 'Noota.mp3'))
print("p")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
    sleep(1)
print ("done")

And although the code loads without displaying an error message, it does not play the music; 而且虽然加载代码时没有显示错误消息,但它不会播放音乐。 instead all I hear is a 'ticking' noise (about one tick per second). 相反,我听到的只是“滴答”声(大约每秒一滴答声)。 I was wondering if anyone knew why this is happening, and how I can fix it. 我想知道是否有人知道为什么会这样,以及我如何解决它。

For reference, I am using python 3 on a mackintosh computer and the music file does play on the computer when loaded using a media player. 作为参考,我在mackintosh计算机上使用python 3,并且在使用媒体播放器加载时,音乐文件确实在计算机上播放。

Thank you! 谢谢!

First, you could try loading the audio file as a pygame.mixer.Sound and use SoundName.play() to check if its not mixer.load() that is broken If it still happens, then its probably an issue with the audio file, go use a universal and up to date format like .ogg so it works on all platforms 首先,您可以尝试将音频文件加载为pygame.mixer.Sound并使用SoundName.play()来检查它是否没有被mixer.load()损坏(如果仍然发生),则可能是音频文件有问题,请使用通用且最新的格式(如.ogg),使其适用于所有平台

Also, time.sleep() may be messing up the sound, as sleep stops every function, and in this case possibly the sound. 同样,由于sleep停止所有功能,因此time.sleep()可能会使声音混乱,在这种情况下可能会停止声音。 Heres an example if you are trying the sound method: 如果您尝试使用声音方法,请参见以下示例:

import os
import pygame
from pygame import *
import sys

GAME_FOLDER = os.path.dirname(__file__)
IMG_FOLDER = os.path.join(GAME_FOLDER, "img")


from time import sleep
noota = pygame.mixer.sound(os.path.join(IMG_FOLDER, 'Noota.mp3'))
print("p")
noota.play()
# Maybe the music is being cut off here v
while pygame.mixer.music.get_busy():
    sleep(1)
print ("done")

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

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