当没有显示视频 (GUI) 时,是否可以允许与 MPV C API 的键绑定?

问题描述

我正在创建一个背景音乐播放器,我想使用 MPV C 插件来执行此操作,但是当我禁用显示视频时出现了问题(使用 check_error(mpv_set_option_string(ctx,"vid","no"));,这可以禁用视频,但后来我不能再使用键(如 q(退出)或 >(跳过))...如何在没有视频 GUI 的情况下允许它们在终端中使用?

我的代码

#include <iostream>
#include <mpv/client.h>

static inline void check_error(int status)
{
    if (status < 0)
    {
        std::cout << "mpv API error: " << mpv_error_string(status) << std::endl;
        exit(1);
    }
}

int main(int argc,char *argv[])
{
    if (argc != 2)
    {
        std::cout << "pass a single media file as argument" << std::endl;

        return 1;
    }

    mpv_handle *ctx = mpv_create();
    if (!ctx)
    {
        std::cout << "Failed creating context" << std::endl;
        return 1;
    }

    check_error(mpv_set_option_string(ctx,"input-default-bindings","yes"));
    mpv_set_option_string(ctx,"input-vo-keyboard","yes");
    int val = 1;
    check_error(mpv_set_option(ctx,"osc",MPV_FORMAT_FLAG,&val));

    check_error(mpv_initialize(ctx));

    const char *cmd[] = {"loadfile",argv[1],NULL};
    check_error(mpv_command(ctx,cmd));

    // THIS IS WHAT I USE TO disABLE THE VIDEO
    // check_error(mpv_set_option_string(ctx,"no"));

    // Let it play,and wait until the user quits.
    while (1)
    {
        mpv_event *event = mpv_wait_event(ctx,10000);
        std::cout << "event: " << mpv_event_name(event->event_id) << std::endl;

        if (event->event_id == MPV_EVENT_SHUTDOWN)
            break;
    }

    mpv_terminate_destroy(ctx);
    return 0;
}

正如您在 mpv_set_option_string(ctx,"yes") 中看到的那样,我允许它使用键绑定,但是如何使键绑定仅在终端上工作,因为它仅在 GUI 可见时才有效?如果您运行:mpv path/to/video.mp3 --no-video,那么即使没有视频 GUI,键绑定仍然可以正常工作。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)