【问题标题】:How do you loop music in PyGame Subset for Android?你如何在 PyGame Subset for Android 中循环播放音乐?
【发布时间】:2019-01-01 22:17:35
【问题描述】:

我一直在尝试让音乐在我用 PyGame Subset 编写的 Android 游戏中循环播放。这是我到目前为止所拥有的,它只播放一次音频文件。我真的很想使用内置的循环功能,这样我就不必使用计时器手动循环音频。有什么想法吗?

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

【问题讨论】:

    标签: android loops pygame playback


    【解决方案1】:

    Note 在 Android 5.x 中不起作用。我知道这是一个旧帖子,但我也遇到了这个问题。由于混合器模块中有错误,zenopython 的答案将进行一些调整。如果您查看 anroid.mixer 模块并查看您将看到的音乐类。

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

    正如你所看到的,这实际上调用了 get_volume(),它总是返回 1.0。为了解决这个问题,我将 Mixer.py 文件复制到我的游戏目录中并将其更改为

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

    然后 imoport 混合器就可以工作了。所以像

    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)
    

    这对我有用。

    如上所述,这在 5.x 中不起作用。

    另一种解决方案是简单地将有效的音乐排队

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

    一般来说pgs4a 中的mixer.music 模块有很多错误。我发现的另一个错误是在mixer.music.pause() 和mixer.music.unpause() 中。 unpause() 只是再次调用 pause() 所以我还必须编辑它以允许在游戏停止时暂停音乐。见下文

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

    【讨论】:

      【解决方案2】:

      也许试试这个而不是 music.load():

      从文件中创建一个新的 Sound 对象

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

      【讨论】:

        【解决方案3】:

        我相当肯定这个问题可以通过mixer.music.get_busy() 解决,它返回一个布尔值,如果正在播放音乐则返回True,如果没有音乐播放则返回False。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-27
          • 2011-08-31
          • 1970-01-01
          相关资源
          最近更新 更多