获取库文件
在官网下载“Dev”版的压缩包,内含`mpv-1.dll`文件。务必从此处下载,这个版本的编译包含i686/x64两种架构,通常使用i686版本是正确的。使用错误版本的.dll文件会导致0xc000007b错误。
编译
以最简单的simple.c为例。目录结构为:
simple-mpv
| simple.c
| mpv-1.dll
| include
| | client.h
| | ...headers
simple.c源代码:
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "include/client.h"
static inline void check_error(int status)
{
if (status < 0)
{
printf("mpv API error: %s\n", mpv_error_string(status));
exit(1);
}
}
int main(int argc, char* argv[])
{
printf("pass a single media file as argument\n");
if (argc != 2)
{
printf("pass a single media file as argument\n");
return 1;
}
mpv_handle* ctx = mpv_create();
if (!ctx)
{
printf("failed creating context\n");
return 1;
}
// Enable default key bindings, so the user can actually interact with
// the player (and e.g. close the window).
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));
// Done setting up options.
check_error(mpv_initialize(ctx));
// Play this file.
const char* cmd[] = { "loadfile", argv[1], NULL };
check_error(mpv_command(ctx, cmd));
// Let it play, and wait until the user quits.
while (1)
{
mpv_event* event = mpv_wait_event(ctx, 10000);
printf("event: %s\n", mpv_event_name(event->event_id));
if (event->event_id == MPV_EVENT_SHUTDOWN)
break;
}
mpv_terminate_destroy(ctx);
return 0;
}
则编译脚本为:
gcc -o simple.exe simple.c mpv-1.dll
此处使用的是MinGW环境,在此环境下的链接器支持直接链接.dll文件。如果使用了正确的mpv-1.dll,则程序应该能正常运行。