libvlc检查媒体位置是否有效

时间:2015-02-01 14:06:49

标签: c++ c libvlc

我使用libvlc并且我想检查媒体位置/路径是否有效:

libvlc_instance_t* inst = libvlc_new(0, NULL);

libvlc_media_t* m = libvlc_media_new_path(inst, "/path/to/nothing");

if (m == NULL)      // Not working
    printf("Err\n");

libvlc_media_player_t* mp = libvlc_media_player_new_from_media(m);

libvlc_media_player_play(mp);

printf("Error: %s\n", libvlc_errmsg()); // (null)

libvlc_media_release(m);
libvlc_media_player_release(mp);
libvlc_release(inst);


return 0;

Libvlc打印一些错误消息,但我无法在自己的代码中发现任何错误:

Error: (null)
[0x7f8cc0004a58] filesystem access error: cannot open file /path/to/nothing (No such file or directory)
[0x7f8cc0004a58] main access error: File reading failed
[0x7f8cc0004a58] main access error: VLC could not open the file "/path/to/nothing" (No such file or directory).

1 个答案:

答案 0 :(得分:2)

在您真正尝试播放媒体之前,有时您无法确定是否存在问题。

libvlc_media_player_play()是异步的,您可以使用LibVLC事件检查错误(或成功)。

创建媒体播放器后,获取事件管理器:

libvlc_event_manager_t* em =
    libvlc.libvlc_media_player_event_manager(mediaPlayer);

然后注册您想要的事件:

libvlc.libvlc_event_attach(
    em, libvlc_MediaPlayerEncounteredError, callback, null);

回调函数是类型为libvlc_callback_t的事件处理程序。

void callback(const struct libvlc_event_t* event, void* userData) {
    if (event->type == libvlc_MediaPlayerEncounteredError) {
        // ...etc...
    }
}