You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用libvlc播放m3u8视频窗口闪烁退出及libvlc_new返回空问题

libvlc播放视频流窗口闪退问题排查

背景

我在采集m3u8索引文件暴露的ts分片后,使用libvlc播放视频流。我此前从未使用过libvlc,参考教程给出的示例代码如下:

libvlc_instance_t* inst;
libvlc_media_player_t* mp;
libvlc_media_t* m;

inst=libvlc_new(0,NULL);
m=libvlc_media_new_path(inst,"...");
mp=libvlc_media_player_new_from_media(m);

libvlc_media_release(m);

// Windows平台下绑定播放窗口
libvlc_media_player_set_hwnd(mp,hwnd);

libvlc_media_player_play(mp);
Sleep(3000);

libvlc_media_player_stop(mp);
libvlc_media_player_release(mp);
libvlc_release(inst);

按教程描述,仅需调用少量函数、设置相关参数完成初始化,再绑定播放窗口即可加载视频资源,libvlc会自动完成编解码工作并播放流,操作完成后释放对应资源即可,使用十分便捷。

问题描述

参照上述代码编写程序后,窗口仅闪烁一次就退出,暂时未找到原因。
我的代码如下:

int test(HWND hwnd)
{
    //const char* version=libvlc_get_version();
    //const char* compiler=libvlc_get_compiler();
    //const char* changeset=libvlc_get_changeset();
    
    //printf("Libvlc.\n");
    //printf("Version:%s\n",version);
    //printf("Compiler:%s\n",compiler);
    //printf("Changeset:%s\n",changeset);
    
    libvlc_instance_t* inst;
    libvlc_media_player_t* mp;
    libvlc_media_t* m;
    
    inst=libvlc_new(0,NULL);
    m=libvlc_media_new_path(inst,"my video resource file path...");
    mp=libvlc_media_player_new_from_media(m);
    
    libvlc_media_release(m);
    
    // Windows平台下绑定播放窗口
    libvlc_media_player_set_hwnd(mp,hwnd);
    
    libvlc_media_player_play(mp);
    Sleep(3000);
    
    libvlc_media_player_stop(mp);
    libvlc_media_player_release(mp);
    libvlc_release(inst);
    
    return 0;
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, PSTR szcmdLine, int icmdshow)
{
    HWND hwnd=NULL;
    
    InitCommonControls();
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    
    if(Frame_Register(hinstance)!=0) {
        MessageBox(NULL,"window register failed","Sorry",MB_OK|MB_ICONERROR);
        return 0;
    }
    if(NULL==(hwnd=Frame_Create(hinstance,1200,600))) return 0;
    
    Frame_Test(hwnd);
    
    return Frame_Run(hwnd);
}

int regist_window(char* class_name,HINSTANCE instance,WNDPROC proc)
{
    WNDCLASSEX wndclass;

    wndclass.cbSize=sizeof(wndclass);
    wndclass.style = CS_DBLCLKS|CS_HREDRAW|CS_VREDRAW;
    wndclass.lpfnWndProc = proc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = instance;
    wndclass.hIcon = NULL;
    wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0,0,0));
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = class_name;
    wndclass.hIconSm = NULL;

    if(!RegisterClassEx(&wndclass)) return -1;
    return 0;
}

HWND Frame_Create(HINSTANCE instance,int width,int height)
{
    int cx,cy;
    POINT pt;
    
    cx=GetSystemMetrics(SM_CXSCREEN);
    cy=GetSystemMetrics(SM_CYSCREEN);
    
    pt.x=(cx-width)>>1;
    pt.y=(cy-height)>>1;
    
    HWND hwnd=CreateWindowEx(NULL,
                             WINDOW_CLASS_NAME,
                             "Docker Frame Capture",
                             WS_OVERLAPPEDWINDOW|WS_VISIBLE|WS_HSCROLL|WS_CLIPCHILDREN,
                             pt.x,pt.y,width,height,NULL,
                             (HMENU)NULL,
                             instance,NULL);

    // 自定义窗口的自绘与数据关联相关操作
    Frame_InitialSettings(hwnd);
    
    return hwnd;
}

int Frame_Run(HWND frame)
{
    MSG msg;

    while(GetMessage(&msg,NULL,0,0)) {
        if(!IsDialogMessage(frame,&msg)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    
    GdiplusShutdown(gdiplusToken);
    
    return(msg.wParam);
}

int Frame_Test(HWND hwnd)
{
    // 创建子窗口作为vlc播放器的容器
    RECT rc;
    GetClientRect(hwnd,&rc);
    int width=rc.right-rc.left,height=rc.bottom-rc.top;
    
    HWND vlc_hwnd=CreateWindowEx(NULL,
                             WC_STATIC,
                             "",
                             WS_CHILD|WS_VISIBLE,
                             0,0,width,height,hwnd,
                             (HMENU)NULL,//LoadMenu(instance,"IDR_FRMENU"),
                             (HINSTANCE)GetWindowLongPtr(hwnd,GWLP_HINSTANCE),NULL);
    
    test(vlc_hwnd);
    return 0;
}

排查信息

经过排查发现libvlc_new函数返回空值,libvlc_errmsg()返回空字符串。

解决方案(7月7日更新)

问题已定位解决,只需将libvlc的plugins文件夹复制到项目目录下即可。
正确的项目文件结构如下:
文件结构说明


内容的提问来源于stack exchange,提问作者Younth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 06:12:02