WPF实现播放器

MainWindow.xaml

<Window
    x:Class="MediaPro.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:MediaPro"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="1200"
    Height="800"
    Loaded="Window_Loaded"
    mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel>
            <Border
                Width="300"
                Height="500"
                Margin="0,20"
                BorderBrush="#9c72d1"
                BorderThickness="2">
                <MediaElement
                    Name="me"
                    LoadedBehavior="Manual"
                    MediaEnded="me_MediaEnded"
                    MediaOpened="me_MediaOpened_1"
                    MouseLeftButtonDown="me_MouseLeftButtonDown"
                    ScrubbingEnabled="True"
                    SpeedRatio="1"
                    Stretch="Fill"
                    StretchDirection="Both"
                    UnloadedBehavior="Stop"
                    Volume="{Binding ElementName=sl_volume, Path=Value}" />
            </Border>
        </StackPanel>
        <StackPanel
            Grid.Row="1"
            Height="120"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Orientation="Horizontal">
            <TextBlock
                Padding="10"
                VerticalAlignment="Center"
                Foreground="#0680d7"
                Text="播放进度" />
            <Slider
                x:Name="sl_progress"
                Width="200"
                Padding="10"
                VerticalAlignment="Center"
                IsMoveToPointEnabled="True"
                Minimum="0"
                PreviewMouseLeftButtonUp="sl_progress_PreviewMouseLeftButtonUp"
                Value="{Binding ElementName=me, Path=Position}" />
            <TextBlock
                Padding="10"
                VerticalAlignment="Center"
                Foreground="#0680d7"
                Text="音量" />
            <Slider
                x:Name="sl_volume"
                Width="150"
                Padding="10"
                VerticalAlignment="Center"
                IsMoveToPointEnabled="True"
                Maximum="1"
                Minimum="0"
                PreviewMouseLeftButtonUp="sl_progress_PreviewMouseLeftButtonUp"
                Value="0.5" />
        </StackPanel>
        <StackPanel
            Grid.Row="2"
            Margin="20,40"
            HorizontalAlignment="Center"
            Orientation="Horizontal">
            <Button
                x:Name="btn_open"
                Width="50"
                Height="30"
                Margin="10,0"
                Click="btn_open_Click"
                Content="打开" />
            <Button
                x:Name="btn_play"
                Width="50"
                Height="30"
                Margin="10,0"
                Click="btn_play_Click"
                Content="播放" />
            <Button
                x:Name="btn_stop"
                Width="50"
                Height="30"
                Margin="10,0"
                Click="btn_stop_Click"
                Content="停止" />
            <Button
                x:Name="btn_back"
                Width="50"
                Height="30"
                Margin="10,0"
                Click="btn_back_Click"
                Content="快退" />
            <Button
                x:Name="btn_foreward"
                Width="50"
                Height="30"
                Margin="10,0"
                Click="btn_foreward_Click"
                Content="快进" />
            <TextBlock
                x:Name="tb_time"
                Margin="10,0"
                VerticalAlignment="Center"
                Text="0:00:00" />
        </StackPanel>
    </Grid>
</Window>

MainWindow.xaml.cs

using Microsoft.Win32;
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 MediaPro
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private System.Timers.Timer timer = null;

        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 打开选择播放文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_open_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = @"视频文件(*.avi格式)|*.avi|视频文件(*.wav格式)
                |*.wav |视频文件(*.wmv)|*.wmv |视频文件(*.mp4格式)|*.mp4|All Files|*.*";
            if (ofd.ShowDialog() == false)
            {
                return;
            }
            string path = "";
            path = ofd.FileName;
            if (path == "")
                return;
            me.Source = new Uri(path, UriKind.Absolute);
            btn_play.IsEnabled = true;
            me.Play();
            timer.Start();
            btn_play.Content = "暂停";
        }

        private void btn_play_Click(object sender, RoutedEventArgs e)
        {
            SetPlayer(true);
            PlayerPause();
        }

        private void btn_stop_Click(object sender, RoutedEventArgs e)
        {
            PlayerStop();
        }

        /// <summary>
        /// 回退10s
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_back_Click(object sender, RoutedEventArgs e)
        {
            me.Pause();
            timer.Stop();
            me.Position = me.Position - TimeSpan.FromSeconds(10);
            SetTime();
            me.Play();
            timer.Start();
        }

        private void btn_foreward_Click(object sender, RoutedEventArgs e)
        {
            me.Pause();
            timer.Stop();
            me.Position = me.Position + TimeSpan.FromSeconds(10);
            SetTime();
            me.Play();
            timer.Start();
        }

        private void changeMax()
        {
            sl_progress.Maximum = me.NaturalDuration.TimeSpan.TotalSeconds;
            tb_time.Text = "0:00:00";
            SetPlayer(true);
        }

        private void me_MediaEnded(object sender, RoutedEventArgs e)
        {
            tb_time.Text = "播放完毕!";
            timer.Stop();
        }

        private void me_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PlayerPause();
        }

        private void sl_progress_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            me.Pause();
            timer.Stop();
            int val = (int)sl_progress.Value;
            me.Position = new TimeSpan(0, 0, 0, val);
            SetTime();
            me.Play();
            timer.Start();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            //每间隔一秒要做的事
            timer.Elapsed += Timer_Elapsed;
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                sl_progress.Value = me.Position.TotalSeconds;
                SetTime();
            }));
        }

        private void SetTime()
        {
            tb_time.Text = string.Format("{0:00}:{1:00}:{2:00}",
                me.Position.Hours, me.Position.Minutes,
                me.Position.Seconds);
        }

        private void PlayerPause()
        {
            if (btn_play.Content.ToString() == "播放")
            {
                me.Play();
                timer.Start();
                btn_play.Content = "暂停";
                me.ToolTip = "点击暂停";
            }
            else
            {
                me.Pause();
                timer.Stop();
                btn_play.Content = "播放";
                me.ToolTip = "点击播放";
            }
        }

        private void SetPlayer(bool bl)
        {
            btn_play.IsEnabled = bl;
            btn_stop.IsEnabled = bl;
            btn_foreward.IsEnabled = bl;
            btn_back.IsEnabled = bl;
        }

        private void PlayerStop()
        {
            timer.Stop();
            me.Stop();
            sl_progress.Value = 0;
            tb_time.Text = "0:00:00";
            btn_play.Content = "播放";
        }

        private void me_MediaOpened_1(object sender, RoutedEventArgs e)
        {
            changeMax();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值