如何在pygame中预订pygame.mixer频道?

2024-05-13 23:06:40 发布

您现在位置:Python中文网/ 问答频道 /正文

预订频道是如何工作的?我可以预订特定频道还是随机选择?没有关于它如何工作的明确文档,我似乎做得不对,因为mixer.findChannel()仍然选择保留通道

这是我的密码:

        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)

我想预订音乐1和音乐2

文档说明mixer.set_reserved()的参数定义了将保留的通道数

如果我不能选择保留哪些频道,有办法吗

提前谢谢


Tags: 文档self密码音乐channel频道pygameset
1条回答
网友
1楼 · 发布于 2024-05-13 23:06:40

有时候,pygame的文档是缺乏的,如果您查看pygame实际调用哪些SDL函数以及这些函数做什么,那么很多东西就更有意义了

因此mixer.set_reserved()实际上calls{a2},但不返回保留通道的数量:

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(){a3}{a4}:

Mix_GroupAvailable

int Mix_GroupAvailable(int tag)

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

如您所见,findChannel忽略保留通道。保留频道仅可防止在使用上述功能之一时自动拾取频道。Pygame使用Mix_PlayChannelTimedMix_FadeInChannelTimed,例如here


总之,如果要确保播放声音,请使用mixer.set_reserved保留一个或多个频道

然后,要播放那个重要的声音,使用mixer.findChannel或者获得一个保留频道或者获得一个无保留的空闲频道。如果您想完全控制频道播放声音,还可以创建一个新的Channel实例,其频道号为0(如果您保留了多个频道,则可以创建更多)

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

因此,您问题中的代码已经实现了您想要的功能:保留通道01

相关问题 更多 >