libvlc 检查媒体位置是否有效

libvlc check if media location is valid

我正在使用 libvlc,我想检查媒体 location/path 是否有效:

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).

有时您只有真正尝试播放媒体才能知道是否存在问题。

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...
    }
}