0

我使用库https://github.com/jaseg/python-mpv来控制 mpv 播放器,但是当它与 pyside6 一起使用时,键绑定不起作用(播放器不完全接受输入)。我究竟做错了什么?还是嵌入pyside6时无法使用它们?(如果我在没有嵌入的情况下使用相同的参数运行播放器,一切正常)

import os
os.add_dll_directory(os.getcwd())
import mpv
from PySide6.QtWidgets import *
from PySide6.QtCore import *
mpvfolderpath = f"mpv.net/portable_config/"
import sys
class Test(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.container = QWidget(self)
        self.setCentralWidget(self.container)
        self.container.setAttribute(Qt.WA_DontCreateNativeAncestors)
        self.container.setAttribute(Qt.WA_NativeWindow)
        player = mpv.MPV(wid=str(int(self.container.winId())),
                         vo="gpu",  # You may not need this
                         log_handler=print,
                         loglevel='debug',
                         input_default_bindings=True,
                         input_vo_keyboard=True)

        @player.on_key_press('f')
        def my_f_binding():
            print("f работает!")
        player.play('test.mp4')

app = QApplication(sys.argv)

# This is necessary since PyQT stomps over the locale settings needed by libmpv.
# This needs to happen after importing PyQT before creating the first mpv.MPV instance.
import locale
locale.setlocale(locale.LC_NUMERIC, 'C')
win = Test()
win.show()
sys.exit(app.exec_())
4

1 回答 1

0

如果未处理键盘(在我的测试中,仅在鼠标悬停视频时发生),则键事件将传播到 Qt 窗口。这意味着我们可以在keyPressEvent()覆盖中处理这些事件,然后创建一个适当的 mpv 命令,该命令已经映射到该keypress()函数。显然,对播放器的引用必须存在,因此您需要将其设为实例属性。

对于标准文字键,通常使用事件的 就足够了text(),但对于其他键(例如箭头),您需要将事件映射到 mpv 的键名。使用字典当然更简单:

MpvKeys = {
    Qt.Key.Key_Backspace:   'BS', 
    Qt.Key.Key_PageUp:      'PGUP', 
    Qt.Key.Key_PageDown:    'PGDWN', 
    Qt.Key.Key_Home:        'HOME', 
    Qt.Key.Key_End:         'END', 
    Qt.Key.Key_Left:        'LEFT', 
    Qt.Key.Key_Up:          'UP', 
    Qt.Key.Key_Right:       'RIGHT', 
    Qt.Key.Key_Down:        'DOWN', 
    # ...
}

class Test(QMainWindow):
    def __init__(self, parent=None):
        # ...
        self.player = mpv.MPV(...)

    def keyPressEvent(self, event):
        # look up for the key in our mapping, otherwise use the event's text
        key = MpvKeys.get(event.key(), event.text())
        self.player.keypress(key)

注意:在我的测试中,我必须使用该vo='x11'标志来正确嵌入窗口,并且osc=True还需要使用本机 OSD。

于 2022-02-23T18:13:44.013 回答