检查PyGame混音器通道是否正在播放声音

时间:2019-01-30 16:07:39

标签: if-statement audio pygame mixer

我正在寻找一种方法来查看pygame.mixer.Channel当前是否正在播放声音。因此,例如,仅在特定频道中播放的声音结束后才执行某些操作。这是我当前的播放声音的代码:

import pygame

pygame.mixer.pre_init()
pygame.mixer.init()
pygame.init()

pygame.mixer.Channel(0).play(pygame.mixer.Sound('coolsound.wav'), maxtime=2000)

我在想一个if语句,像这样:

if pygame.mixer.Channel(0) = playing a sound:
    print("playing a sound")
else:
    print("not playing")

这显然行不通,只是让您对我正在寻找的东西有所了解。 谢谢!

1 个答案:

答案 0 :(得分:1)

我得到了答案。使用get_busy()可以检查频道是否正在播放声音,它返回 ExecutorService executorService = null; //List<MyObject> allResults = new ArrayList<MyObject>(); try { // Initialize the executor service executorService = Executors.newFixedThreadPool(5); // Process numbers for(String phoneNumber : allPhoneNumbers) { executorService.execute(new Runnable() { @Override public void run() { logger.info("About to process: " + phoneNumber); sendSms(phoneNumber, amqpMessage.getMessage()); // This would return MyObject = sendSms(...) } }); } // No more threads can be added executorService.shutdown(); // Wait until threads are finished and terminate executorService.awaitTermination(amqpMessage.getAllPhoneNumbersGettingMessage().size(), TimeUnit.MINUTES); True。这是我的最终代码:

False

这里是文档链接,以获取更多信息:https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.get_busy