How do you make music in PyGame Subset for Android?

I'm trying to get music to loop in my Android game written by a subset of PyGame. This is what I still have, and it only plays the audio file once. I would really like to use the built-in loop function, so I do not need to manually encode the audio using a timer. Any ideas?

import pygame try: import pygame.mixer as mixer except ImportError: import android_mixer as mixer if mixer: mixer.music.load("woo.mp3") mixer.music.play(-1) while True: if mixer: mixer.periodic() 
+4
source share
3 answers

Note does not work in Android 5.x. I know this is an old post, but I also ran into this problem. Zenopython's answer will work with some tweaking since there is an error in the mixer module. If you look in the anroid.mixer module and look at the music class that you will see.

 @staticmethod def get_busy(): return music_channel.get_volume() 

as you can see, it actually calls get_volume (), which always returns 1.0. To fix this, I copied the mixer.py file to my games directory and changed it to

  @staticmethod def get_busy(): return music_channel.get_busy() 

Then imoport mixer and it works. Sort of

 import pygame try: import pygame.mixer as mixer except ImportError: import mixer if mixer: mixer.music.load("woo.mp3") mixer.music.play(-1) while True: if mixer: if mixer.music.get_busy() == False: mixer.music.play(-1) 

It worked for me.

As mentioned above, this does not work in 5.x.

Another solution is simply a queue of music that works

 mixer.music.load('music') mixer.music.play() mixer.music.queue('music') 

In general, the mixer.music module in pgs4a has a number of errors. Another error I discovered is in mixer.music.pause () and mixer.music.unpause (). unpause () calls pause () again, so I also had to edit this to pause music when the game was stopped. See below

 if android: if android.check_pause(): mixer.music.pause() android.wait_for_resume() mixer.music.unpause() 
+1
source

Perhaps try this instead of music.load ():

To create a new Sound object from a file

 mixer.Sound(filename): return Sound Sound.play(-1) 
0
source

I am sure that the problem can be solved with mixer.music.get_busy (), which returns bool, True if the music is playing, False if there is no music.

 import pygame try: import pygame.mixer as mixer except ImportError: import android_mixer as mixer if mixer: mixer.music.load("woo.mp3") mixer.music.play(-1) while True: if mixer: if mixer.music.get_busy() == False: mixer.music.play() 
0
source

Source: https://habr.com/ru/post/1385680/


All Articles