summaryrefslogtreecommitdiffstats
path: root/DOCS
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-02-10 21:30:55 +0100
committerwm4 <wm4@nowhere>2014-02-10 21:30:55 +0100
commita6da2a66080a53d5885446ea9d788470c7b259d2 (patch)
tree683a97850192a6a106a4e4a325f76ba626f05fa4 /DOCS
parent3dd12104d9c021a10ba92680896edb777ae852d3 (diff)
downloadmpv-a6da2a66080a53d5885446ea9d788470c7b259d2.tar.bz2
mpv-a6da2a66080a53d5885446ea9d788470c7b259d2.tar.xz
Add a client API example
Diffstat (limited to 'DOCS')
-rw-r--r--DOCS/client_api_examples/shared.h10
-rw-r--r--DOCS/client_api_examples/simple.c43
2 files changed, 53 insertions, 0 deletions
diff --git a/DOCS/client_api_examples/shared.h b/DOCS/client_api_examples/shared.h
new file mode 100644
index 0000000000..027c03f1e9
--- /dev/null
+++ b/DOCS/client_api_examples/shared.h
@@ -0,0 +1,10 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+static inline void check_error(int status)
+{
+ if (status < 0) {
+ printf("mpv API error: %s\n", mpv_error_string(status));
+ exit(1);
+ }
+}
diff --git a/DOCS/client_api_examples/simple.c b/DOCS/client_api_examples/simple.c
new file mode 100644
index 0000000000..c5d8dc175c
--- /dev/null
+++ b/DOCS/client_api_examples/simple.c
@@ -0,0 +1,43 @@
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "libmpv/client.h"
+#include "shared.h"
+
+int main(int argc, char *argv[])
+{
+ 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"));
+ check_error(mpv_set_option_string(ctx, "osc", "yes"));
+
+ // 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_destroy(ctx);
+ return 0;
+}