1

我正在创建一个背景音乐播放器,我想使用 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, "vid", "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, "input-default-bindings", "yes")我允许它使用键绑定,但是如何使键绑定仅在终端上工作,因为它仅在 GUI 可见时才有效?如果您运行:mpv path/to/video.mp3 --no-video那么即使没有视频 GUI,键绑定仍然可以正常工作。

4

0 回答 0