Pygame无法在未连接显示器的Mac mini上运行

时间:2014-01-19 09:19:47

标签: python macos opengl pygame

我正在制作一个Makey Makey项目,但出于这个问题的目的,它可以被视为一个简单的键盘。

我制作了一个显示该项目的YouTube视频,除了屏幕黑客外,我想要相同的功能: http://youtu.be/98ATkZUR48k

我把它连接到我的厨房橱柜上,所以当我打开其中一个时,它会播放一首歌。

我将它连接到我的Mac mini,只要它连接了一个屏幕就可以正常工作。 当我拔下屏幕时,歌曲停止播放。

我已经读过,根据是否连接了显示器,Mac mini会加载不同的图形驱动程序,特别是不支持OpenGL的显示器。

这是我对它无法正常工作的直觉,但我不确定。

所以我的问题是pygame是否需要运行OpenGL,有没有办法禁用它?

我发现的唯一的东西是这些硬件解决方案,对于我的用例,我认为是矫枉过正: https://macminicolo.net/blog/files/build-a-dummy-dongle-for-a-headless-mac-mini

更新:


正如@Torxed所指出的,Pygame严重依赖OpenGL,所以也许另一个模块或多个模块的组合会更有用。

我选择了Pygame,因为我可以:

  • 轻松获取循环键盘输入
  • 一次播放多个声音文件
  • 能够暂停和恢复声音文件

你能推荐一个模块或一组模块,可以帮助我轻松完成这两个功能。

这是我现在使用pygame运行的代码:

import sys, pygame
from pygame.mixer import Sound, Channel

class MakeyCupboard :
    channelIdCounter = 0
    def __init__(self, fileName):
        self.channelId = MakeyCupboard.channelIdCounter
        MakeyCupboard.channelIdCounter = MakeyCupboard.channelIdCounter + 1
        self.channel = Channel(MakeyCupboard.channelIdCounter)
        self.sound = Sound(fileName)
        self.channel.play(self.sound, -1)
        self.channel.pause()


pygame.init()

dictCupboard = {
    pygame.K_w : MakeyCupboard("rien.ogg"),
    pygame.K_a : MakeyCupboard("train.ogg"),
    pygame.K_s : MakeyCupboard("stronger.ogg")
}

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN :
            try : 
                makeyCupboard = dictCupboard[event.key]
                makeyCupboard.channel.pause()
            except KeyError:
                pass

        elif event.type == pygame.KEYUP :
            try:
                makeyCupboard = dictCupboard[event.key]
                makeyCupboard.channel.unpause()
            except KeyError:
                pass

1 个答案:

答案 0 :(得分:1)

如果您只对播放声音感兴趣,这是您问题的一种解决方案:

from subprocess import Popen, STDOUT, PIPE
from time import sleep
audio_file = "/tmp/music.wav"

x = Popen('afplay ' + audio_file, shell=True, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
while x.poll() == None:
    output = x.stdout.readline() # You need this!
    # Otherwise the output buffer will get full and hang your application.
    # Or just remove stdout=PIPE, stdin=PIPE, stderr=STDOUT from the Popen() call
    # if you're not interested in the output of the application.
    sleep(0.025)    
print('Music stoped')

更新

由于您要暂停/切换曲目,您需要通过x.stdin.write('next\n')发送命令,或者afplay将命令作为命令发送。

另一个解决方案是使用其中一个库here,例如:

从外观来看,PyAudio看起来很简单:

import pyaudio
import wave
import sys

CHUNK = 1024

if len(sys.argv) < 2:
    print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
    sys.exit(-1)

wf = wave.open(sys.argv[1], 'rb')

p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)

data = wf.readframes(CHUNK)

while data != '':
    stream.write(data)
    data = wf.readframes(CHUNK)

stream.stop_stream()
stream.close()

p.terminate()