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

2024-07-02 11:19:54 发布

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

我正在做一个Makey-Makey项目,但是对于这个问题来说,它可以被认为是一个简单的键盘。你知道吗

我制作了一个YouTube视频来展示这个项目,除了屏幕上的黑客行为之外,我想要同样的功能: http://youtu.be/98ATkZUR48k

我把它挂在厨房的碗橱上,所以当我打开其中一个碗橱时,它就会播放一首歌。你知道吗

我把它连接到我的macmini上,只要它有一个连接的屏幕就可以正常工作。 当我拔掉屏幕上的插头时,歌曲就停止播放了。你知道吗

我读到macmini会根据是否连接了显示器加载不同的图形驱动程序,尤其是不支持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

Tags: 模块项目selfevent屏幕typechannelpygame
1条回答
网友
1楼 · 发布于 2024-07-02 11:19:54

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

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')或w/e发送命令,它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()

相关问题 更多 >