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();
}
}
}
备注
需将视频文件存放在程序的输出路径下。