wpf编写简单的视频播放器

这篇博客介绍了如何利用WPF(Windows Presentation Foundation)框架编写一个基本的视频播放器。通过MainWindow.xaml文件配置UI,设置MediaElement控件来显示视频,并在MainWindow.xaml.cs中初始化播放器并响应播放按钮事件,实现视频的播放功能。用户需将视频文件放置在程序的输出路径下以供播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WPF编写简单的视频播放器

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid MinWidth="340" Height="360">
        <Grid.RowDefinitions>
        	<RowDefinition Height="20">
        	<RowDefinition Height="*">
        </Grid.RowDefinitions>
        <Button Click="playmedia" width="30" Height="15" HorizontalAligment="Left" Grid.Row="0"/>
        <MediaElement Grid.Row="1" x:Name="Player" Width="340" Height="320" IsMuted="True" Volume="3.5" LoadedBehavior="Manual"/> 
    </Grid>
</Window>

LoadedBehavior="Manual"
可手动控制视频播放状态

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Player.Source = new uri(AppDomain.CurrentDomain.BaseDirectory+"XX.mp4");
        }
        private void playmedia(object sender,RoutedEventArgs e)
        {
			this.Player.Play();
		}
    }
}

备注

需将视频文件存放在程序的输出路径下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值