python - 如何在 pygame 中保留 pygame.mixer channel ?

标签 python audio pygame mixer

预订 channel 如何运作?我可以保留特定 channel 还是随机选择?没有关于它如何工作的明确文档,我似乎做错了,因为 mixer.findChannel() 仍然选择保留 channel 。

这是我的代码:

        self.music1 = pygame.mixer.Channel(0)
        self.music2 = pygame.mixer.Channel(1)
        self.sound1 = pygame.mixer.Channel(2)
        self.sound2 = pygame.mixer.Channel(3)
        self.sound3 = pygame.mixer.Channel(4)
        self.sound4 = pygame.mixer.Channel(5)
        self.sound5 = pygame.mixer.Channel(6)
        self.sound6 = pygame.mixer.Channel(7)
        
        pygame.mixer.set_reserved(2)

我想预订 music1 和 music2。

文档指出 mixer.set_reserved() 的参数定义了将被保留的 channel 数。

如果我无法选择要保留哪些 channel ,是否有解决办法?

提前致谢

最佳答案

有时,pygame 的文档是缺乏的,如果您查看 pygame 实际调用的 SDL 函数以及这些函数的作用,很多东西会更有意义。

所以 mixer.set_reserved() 实际上是 calls Mix_ReserveChannels ,但不返回预留 channel 数:

Mix_ReserveChannels

int Mix_ReserveChannels(int num)

Reserve num channels from being used when playing samples when passing in -1 as a channel number to playback functions. The channels are reserved starting from channel 0 to num-1. Passing in zero will unreserve all channels. Normally SDL_mixer starts without any channels reserved.

The following functions are affected by this setting:
4.3.3 Mix_PlayChannel
4.3.4 Mix_PlayChannelTimed
4.3.5 Mix_FadeInChannel
4.3.6 Mix_FadeInChannelTimed

mixer.findChannel() calls Mix_GroupAvailable :

Mix_GroupAvailable

int Mix_GroupAvailable(int tag)

Find the first available (not playing) channel in group tag.

如您所见,findChannel 忽略保留的 channel 。保留 channel 仅可防止在使用上述功能之一时自动选择 channel 。 Pygame 使用 Mix_PlayChannelTimedMix_FadeInChannelTimed,例如 here .


综上所述,如果您想确保播放声音,请使用 mixer.set_reserved 保留一个或多个 channel 。

然后,要播放那个重要的声音,请使用 mixer.findChannel获得预留 channel 获得未预留的免费 channel .如果您想完全控制 channel ,您还可以创建一个 channel 号为 0 的新 Channel 实例(如果您保留了多个 channel ,则可以创建更多 channel )播放声音。

在不指定 channel 的情况下播放所有其他声音,它们将只在非保留 channel 上播放。

所以您问题中的代码已经做了您想要的:保留 channel 01

关于python - 如何在 pygame 中保留 pygame.mixer channel ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66228684/

相关文章:

python - 使用astWCS尝试创建WCS对象时出错

python - matplotlib 的 pyplot() 和 pandasplot() 之间的区别?

python - Pygame sprite 无效的 rectstyle 对象

python - 使用 'Blit' Pygame 无法将图像打印到屏幕

python - 如何在单元格为空/空时通过 python 在 google 工作表中插入值

c++ - 如何合并(混合)两个立体声 mp3 音频文件

Linux:如何设置一个应用程序使用不同的声卡?

php - 检测文件是否是PHP中没有mime类型的音频文件

python - 在安装的pygame目录中哪里可以找到pygame.init()方法?

python - 如何在没有 Sprite 的情况下在Pygame中实现跳跃?