WPF+LibVLC开发播放器-播放时间显示

播放时间显示

界面

增加一个TextBlock用于时间显示

xml 复制代码
<TextBlock
    Name="TimeText"
    Grid.Row="1"
    Margin="131,0,0,9"
    HorizontalAlignment="Left"
    VerticalAlignment="Bottom"
    Text="TextBlock"
    TextWrapping="Wrap" />

代码

MediaPlayer增加一个时间变化事件TimeChanged

csharp 复制代码
public partial class MainWindow : Window
{
    private LibVLC _libVLC;

    private MediaPlayer _player;

    public MainWindow()
    {
        InitializeComponent();
        Core.Initialize();
        _libVLC = new LibVLC();
        _player = new MediaPlayer(_libVLC);
        //时间变化事件
        _player.TimeChanged += Player_TimeChanged;
        
        videoView.MediaPlayer = _player;

      
    }
    
}

 private void Player_TimeChanged(object? sender, MediaPlayerTimeChangedEventArgs e)
 {

 }

相关属性介绍

  1. Time属性,获取当前播放时间
    1. 类型:long
    2. 单位:ms
    3. 设置Time属性可以修改当前播放进度
  2. Length属性,当前视频总的时长
    1. 类型:long
    2. 单位:ms
    3. 只读

实现

通过TimeSpan.FromMilliseconds对time和length属性进行转换,然后转成对应的string格式

需要注意的是这个事件是线程中调用,修改控件需要跨线程调用,需要this.Dispatcher.Invoke来处理

csharp 复制代码
 private void Player_TimeChanged(object? sender, MediaPlayerTimeChangedEventArgs e)
 {
   Dispatcher.Invoke(
       new Action(() =>
       {

      TimeText.Text =
           TimeSpan.FromMilliseconds(_player.Time).ToString(@"hh\:mm\:ss")
           + "/"
           + TimeSpan.FromMilliseconds(_player.Length).ToString(@"hh\:mm\:ss");//总长
           
       })
   );
 }

实现效果

视频教程

WPF+LibVLC开发播放器-时间进度显示

相关推荐
SamChan9014 小时前
Python+PyMuPDF提取PDF表格并翻译:表格结构检测与双语重建实战
windows·python·ai·pdf·wpf
SamChan9015 小时前
Python+OpenCV检测PDF翻译后排版偏移:像素级格式一致性验证
python·opencv·ai·pdf·wpf
FuckPatience16 小时前
WPF 拿到控件的最初样式,修改获得焦点样式
wpf
主宰者16 小时前
【WPF】MainWindow前添加加载窗口,发现加载过程中有白色无内容窗口闪烁一下的解决方案
wpf
Vae_Mars2 天前
WPF中的ControlTemplate(控件模板)
wpf
SamChan902 天前
Python+ReportLab自动生成PDF翻译质量审计报告:从数据到可视化的完整方案
开发语言·python·ai·pdf·wpf
circuitsosk2 天前
多智能体协同在能源数据分析中的实践:分配-执行-校验闭环架构设计
python·数据分析·wpf·能源·并行计算·多智能体系统·agent协作
weixin199701080163 天前
《大促护航:电商API限流与降级,双11峰值500万调用的架构复盘》(附Python源码)
python·架构·wpf
hh9503 天前
gent Plan x DeepSeek Harness:多 Agent 协作场景下的中央编排实践
人工智能·wpf·adg·火山引擎·agent plan·adg成都社区
Vae_Mars3 天前
WPF中的Lazy<T>使用方法
wpf