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

使用Vlc.DotNet连续播放视频时出现卡顿的问题咨询

解决Vlc.DotNet连续播放视频卡顿的方法

1. 保证VlcControl操作在UI线程执行

Vlc.DotNet的控件操作必须在UI线程进行,你当前用ThreadPool调用Play方法属于跨线程操作,会导致控件状态异常,进而引发卡顿。修改播放逻辑,通过UI线程调度执行:

int ListNum = 0;
List<string> PlayList = new List<string>();
string[] AllFiles;

private void PlayVedioFiles() {
    vlcControl1.Audio.Volume = 0;
    ListNum = 0;
    AllFiles = Directory.GetFiles("F:\\VedioFiles");
    PlayList.Clear();
    PlayList.AddRange(AllFiles);
    if (PlayList.Count > 0) {
        vlcControl1.Play(new FileInfo(PlayList[ListNum]));
    }
}

private void vlcControl1_EndReached(object sender, Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e) {
    ListNum = (ListNum + 1) % PlayList.Count;
    this.Invoke((Action)(() => {
        vlcControl1.Play(new FileInfo(PlayList[ListNum]));
    }));
}

2. 预加载下一个媒体文件

提前预加载下一个视频的媒体实例,减少切换时的初始化耗时,避免卡顿。可以在当前视频播放结束前或初始化阶段准备下一个媒体:

private VlcMedia _nextMedia;

private void vlcControl1_EndReached(object sender, Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e) {
    ListNum = (ListNum + 1) % PlayList.Count;
    this.Invoke((Action)(() => {
        if (_nextMedia != null) {
            vlcControl1.Play(_nextMedia);
            PrepareNextMedia();
        } else {
            vlcControl1.Play(new FileInfo(PlayList[ListNum]));
            PrepareNextMedia();
        }
    }));
}

private void PrepareNextMedia() {
    int nextIndex = (ListNum + 1) % PlayList.Count;
    _nextMedia = new VlcMedia(vlcControl1.MediaPlayer, new FileInfo(PlayList[nextIndex]));
    _nextMedia.Parse();
}

private void PlayVedioFiles() {
    vlcControl1.Audio.Volume = 0;
    ListNum = 0;
    AllFiles = Directory.GetFiles("F:\\VedioFiles");
    PlayList.Clear();
    PlayList.AddRange(AllFiles);
    if (PlayList.Count > 0) {
        vlcControl1.Play(new FileInfo(PlayList[ListNum]));
        if (PlayList.Count > 1) {
            PrepareNextMedia();
        }
    }
}

3. 优化播放参数

通过设置媒体选项调整缓存或解码逻辑,适配硬件环境:

var mediaOptions = new string[] {
    ":file-caching=1000",
    ":network-caching=1000"
};

// 播放时传入参数
vlcControl1.Play(new FileInfo(PlayList[ListNum]), mediaOptions);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 07:52:37