简体   繁体   中英

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.

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

Also, time.sleep() may be messing up the sound, as sleep stops every function, and in this case possibly the sound. 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")

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