在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 播放mp4/avi/wmv视频文件示例源码(wpf)

C# 播放mp4/avi/wmv视频文件示例源码(wpf)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.30M
  • 下载次数:1
  • 浏览次数:6
  • 发布时间:2018-02-11
  • 实例类别:C#语言基础
  • 发 布 人:951370275
  • 所需积分:2
 相关标签: C# 视频 播放

实例介绍

【实例简介】支持的视频文件有(*.avi,*.mp4,*.wmv)

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using MultiMediaStudio.UserInfoClass;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using System.Threading;
using Microsoft.Win32;

namespace MultiMediaStudio.UserContorl.Interface
{
    /// <summary>
    /// UserPlayerSence.xaml 的交互逻辑
    /// </summary>
    public partial class UserPlayerSence : UserControl
    {
        public UserPlayerSence()
        {
            InitializeComponent();
            mTimer = new DispatcherTimer();
            mTimer.Interval = TimeSpan.FromMilliseconds(100);
            mTimer.Tick  = new EventHandler(mTimer_Tick);
        }
        PlayerVideoInfo mPlayerVideoInfo;
        public event Action BackMainSence;
        private int mIndex =0;
        double soundNow = 1;
        double mVideoLong = 0;
        private bool mVideoEnd = false;
        private bool mBoolOpen = false;
        enum stateVideo
        {
            none,
            play,
            pause
        }
        private bool mBoolMove = false;
        stateVideo mstateVideo = stateVideo.none;
        private DispatcherTimer mTimer;
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GlobalManager.LayoutElement(0, 0, 1366, 768, MainCanvas);
            GlobalManager.LayoutElement(0, -30, 1366, 743, borderMedia);
            GlobalManager.LayoutElement(2, 2, 1362, 739, MainMedia);
            GlobalManager.LayoutElement(0, 683, 1366, 85, ButtomCanvas);
            GlobalManager.LayoutElement(0, 0, 1366, 85, BottomBG);
            GlobalManager.LayoutElement(23, 10, 72, 72, PlayBg);
            GlobalManager.LayoutElement(23, 10, 72, 72, PauseBg);
            PauseBg.Visibility = Visibility.Collapsed;
            GlobalManager.LayoutElement(165, 39, 648, 16, PostionBG);
            GlobalManager.LayoutElement(165, 39, 648, 16, PostionFont);
            PostionFont.Width = 0;

            GlobalManager.LayoutElement(155, 37, 20, 23, PostionNow);
            GlobalManager.LayoutElement(908, 33, 30, 30, SoundImageBG);
            GlobalManager.LayoutElement(957, 41, 193, 13, SoundBG);
            GlobalManager.LayoutElement(957, 41, 193, 13, SoundFont);
            SoundFont.Width = 0;
            GlobalManager.LayoutElement(951.5, 40, 13, 19, SoundNow);
            GlobalManager.LayoutElement(1256, 20, 89, 45, BackImageBG);
            GlobalManager.LayoutElement(633, 291, 100, 100, RunImage);

            GlobalManager.LayoutElement(635, 339, 110, 65, TJ);
            RunImage.Visibility = Visibility.Collapsed;
            Canvas.SetZIndex(RunImage, 50);
            InitVolume();
            
            RunImage.RenderTransformOrigin = new Point(0.5, 0.5);   
            MainMedia.MediaOpened =new RoutedEventHandler(MainMedia_MediaOpened);
        }
        int mAngel = 0;
        void mTimer_Tick(object sender, EventArgs e)
        {
            if (mAngel >= 360)
                mAngel = 0;
            mAngel = mAngel   30;
            RunImage.RenderTransform = new RotateTransform(mAngel);
        }
        #region 初始化
        public void InitSence(PlayerVideoInfo playerVideoInfo)
        {
            mPlayerVideoInfo = playerVideoInfo;
            BottomBG.Source = playerVideoInfo.PlayButtonList[0];
            PlayBg.Source = playerVideoInfo.PlayButtonList[1];
            PauseBg.Source = playerVideoInfo.PlayButtonList[2];
            PostionBG.Source = playerVideoInfo.PlayButtonList[3];
            PostionFont.Source = playerVideoInfo.PlayButtonList[4];
            PostionNow.Source = playerVideoInfo.PlayButtonList[5];
            SoundImageBG.Source = playerVideoInfo.PlayButtonList[6];
            SoundBG.Source = playerVideoInfo.PlayButtonList[7];
            SoundFont.Source = playerVideoInfo.PlayButtonList[8];
            SoundNow.Source = playerVideoInfo.PlayButtonList[9];
            BackImageBG.Source = playerVideoInfo.PlayButtonList[10];
            RunImage.Source = playerVideoInfo.PlayButtonList[11];
            TJ.Source = playerVideoInfo.PlayButtonList[12];
        }
        #endregion

        public void SetPlay(int index)
        {
            RunImage.Visibility = Visibility.Visible;
            mAngel = 0;
            mTimer.Start();
            mIndex = index - 1;

            MainMedia.Source = mPlayerVideoInfo.UriVideo[index -1];
            MainMedia.Play();
            mstateVideo = stateVideo.play;
        }
        public void SetPlay(string index)
        {
            //PauseBg.Visibility = Visibility.Visible;
            
            TJ.Visibility = Visibility.Collapsed;
            RunImage.Visibility = Visibility.Visible;
            mAngel = 0;
            mTimer.Start();
            MainMedia.Source = new Uri(index);
            MainMedia.Play();
            mstateVideo = stateVideo.play;
        }
        private void InitVolume()
        {
            soundNow = 1;
            Canvas.SetLeft(SoundNow, GlobalManager.TransValueFromWidth(951.5)   soundNow * GlobalManager.TransValueFromWidth(193));
            SoundFont.Width = soundNow * GlobalManager.TransValueFromWidth(193);
            MainMedia.Volume = soundNow;
        }

        private void BackImageBG_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (BackMainSence != null)
            {
                MainMedia.Stop();
                MainMedia.Source = null;
                InitVolume();
                mStory.Stop();
                PostionFont.Width = 0;
                mBoolOpen = false;
                Canvas.SetLeft(PostionNow, GlobalManager.TransValueFromWidth(155));
                mVideoEnd = false;
                PlayBg.Visibility = Visibility.Collapsed;
                PauseBg.Visibility = Visibility.Visible;
                ButtonState.mState = ButtonState.States.None;

                mTimer.Stop();
                RunImage.Visibility = Visibility.Collapsed;
                BackMainSence();
            }
        }

        private void PlayBg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (mVideoEnd)
            {
                MainMedia.Source = mPlayerVideoInfo.UriVideo[mIndex];
                mVideoEnd = false;
            }
            else
            {
                mStory.Resume();
            }
            MainMedia.Play();
            if (mBoolMove)
            {
                Move(PostionFont, PostionNow);
            }
            mstateVideo = stateVideo.play;
            PauseBg.Visibility = Visibility.Visible;
            PlayBg.Visibility = Visibility.Collapsed;
        }

        private void PauseBg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            mStory.Pause();
            MainMedia.Pause();
            mstateVideo = stateVideo.pause;
            PauseBg.Visibility = Visibility.Collapsed;
            PlayBg.Visibility = Visibility.Visible;
        }

        private void MainMedia_MediaEnded(object sender, RoutedEventArgs e)
        {
            PauseBg.Visibility = Visibility.Collapsed;
            PlayBg.Visibility = Visibility.Visible;
            MainMedia.Stop();
            MainMedia.Source = null;
            mStory.Stop();
            mBoolOpen = false;
            PostionFont.Width = 0;
            Canvas.SetLeft(PostionNow, GlobalManager.TransValueFromWidth(155));
            mVideoEnd = true;
            //MainMedia.Source = mPlayerVideoInfo.UriVideo[mIndex];
        }

        private void SoundBG_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (ButtonState.mState == ButtonState.States.None && mBoolOpen)
            {
                ButtonState.mState = ButtonState.States.Down;
                Canvas.SetLeft(SoundNow,e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(6.5));
                SoundFont.Width = e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(957);
                soundNow = (e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(957)) / GlobalManager.TransValueFromWidth(193);
                MainMedia.Volume = soundNow;
            }
            
        }

        private void SoundBG_MouseMove(object sender, MouseEventArgs e)
        {
            if (ButtonState.mState == ButtonState.States.Down)
            {
                Canvas.SetLeft(SoundNow, e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(6.5));
                SoundFont.Width = e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(957);
                soundNow = (e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(957)) / GlobalManager.TransValueFromWidth(193);
                MainMedia.Volume = soundNow;
            }
        }

        private void SoundBG_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (ButtonState.mState == ButtonState.States.Down)
            {
                ButtonState.mState = ButtonState.States.Up;
                ButtonState.mState = ButtonState.States.None;
            }
        }

        private void SoundBG_MouseLeave(object sender, MouseEventArgs e)
        {
            if (ButtonState.mState == ButtonState.States.Down)
            {
                ButtonState.mState = ButtonState.States.Leave;
                ButtonState.mState = ButtonState.States.None;
            }
        }

        private void MainMedia_MediaOpened(object sender, RoutedEventArgs e)
        {
            mTimer.Stop();
            PauseBg.Visibility = Visibility.Visible;
            PlayBg.Visibility = Visibility.Collapsed;
            RunImage.Visibility = Visibility.Collapsed;
            mVideoLong = MainMedia.NaturalDuration.TimeSpan.TotalMilliseconds;
            mTime = mVideoLong;
            mBoolOpen = true;
            Move(PostionFont,PostionNow);
            Console.WriteLine(mVideoLong/PostionBG.Width);
        }

        Storyboard mStory;
        double mTime = 1;
        bool mDoubleAnimtion = true;
        private void Move(FrameworkElement elem, FrameworkElement elem2)
        {
            if (mTime > 0)
            {
                mStory = new Storyboard();
                DoubleAnimation width = new DoubleAnimation(PostionBG.Width, new Duration(TimeSpan.FromMilliseconds(mTime)), FillBehavior.Stop);
                Storyboard.SetTarget(width, elem);
                Storyboard.SetTargetProperty(width, new PropertyPath(FrameworkElement.WidthProperty));
                DoubleAnimation left = new DoubleAnimation(PostionBG.Width   GlobalManager.TransValueFromWidth(155), new Duration(TimeSpan.FromMilliseconds(mTime)), FillBehavior.Stop);
                Storyboard.SetTarget(left, elem2);
                Storyboard.SetTargetProperty(left, new PropertyPath(Canvas.LeftProperty));
                mStory.FillBehavior = FillBehavior.Stop;
                mStory.Children.Add(width);
                mStory.Children.Add(left);
                mStory.Completed  = delegate
                {
                    elem.Width = PostionBG.Width;
                    Canvas.SetLeft(elem2, PostionBG.Width   GlobalManager.TransValueFromWidth(155));
                    mBoolMove = false;
                };
                mStory.Begin();
            }
            
        }
        private void InstantMove(FrameworkElement elem, FrameworkElement elem2,double to, double time)
        {
            mDoubleAnimtion = false;
            mStory = new Storyboard();
            DoubleAnimation width = new DoubleAnimation(to, new Duration(TimeSpan.FromMilliseconds(time)), FillBehavior.Stop);
            Storyboard.SetTarget(width, elem);
            Storyboard.SetTargetProperty(width, new PropertyPath(FrameworkElement.WidthProperty));
            DoubleAnimation left = new DoubleAnimation(to   GlobalManager.TransValueFromWidth(155), new Duration(TimeSpan.FromMilliseconds(time)), FillBehavior.Stop);
            Storyboard.SetTarget(left, elem2);
            Storyboard.SetTargetProperty(left, new PropertyPath(Canvas.LeftProperty));
            mStory.FillBehavior = FillBehavior.Stop;
            mStory.Children.Add(width);
            mStory.Children.Add(left);
            mStory.Completed  = delegate
            {
                elem.Width = to;
                mDoubleAnimtion = true;
                Canvas.SetLeft(elem2, to   GlobalManager.TransValueFromWidth(155));
                if (mstateVideo == stateVideo.play)
                {
                    Move(PostionFont, PostionNow);
                }
                else
                {
                    mBoolMove = true;
                }
            };
            mStory.Begin();
        }


        private void PostionBG_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (ButtonState.mState == ButtonState.States.None && mBoolOpen)
            {
                ButtonState.mState = ButtonState.States.Down;
                
                mStory.Stop();
                double movewidth = e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(165);
                MainMedia.Position = TimeSpan.FromMilliseconds(mVideoLong * movewidth / PostionBG.Width);
                mTime = mVideoLong - mVideoLong * (e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(165)) / PostionBG.Width ;
                InstantMove(PostionFont, PostionNow, movewidth, 100);
            }
        }

        private void PostionBG_MouseLeave(object sender, MouseEventArgs e)
        {
            if (ButtonState.mState == ButtonState.States.Move && mDoubleAnimtion)
            {
                ButtonState.mState = ButtonState.States.Leave;
                mStory.Stop();
                double movewidth = e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(165);
                MainMedia.Position = TimeSpan.FromMilliseconds(mVideoLong * movewidth / PostionBG.Width);
                mTime = mVideoLong - mVideoLong * (e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(165)) / PostionBG.Width;
                if (mstateVideo == stateVideo.play)
                {
                    MainMedia.Play();
                    //InstantMove(PostionFont, PostionNow, movewidth, 100);
                    //Thread.Sleep(100);
                    Move(PostionFont, PostionNow);
                }
                else
                {
                    mBoolMove = true;
                }
                ButtonState.mState = ButtonState.States.None;
            }
            else if (ButtonState.mState == ButtonState.States.Down)
            {
                ButtonState.mState = ButtonState.States.Leave;
                ButtonState.mState = ButtonState.States.None;
            }
        }

        private void PostionBG_MouseMove(object sender, MouseEventArgs e)
        {
            if (ButtonState.mState == ButtonState.States.Down || ButtonState.mState == ButtonState.States.Move)
            {
                if (mDoubleAnimtion)
                {
                    MainMedia.Pause();
                    mStory.Stop();
                    double movewidth = e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(165);
                    //MainMedia.Position = TimeSpan.FromMilliseconds(mVideoLong * movewidth / PostionBG.Width);
                    Canvas.SetLeft(PostionNow, movewidth   GlobalManager.TransValueFromWidth(155));
                    PostionFont.Width = movewidth;
                    ButtonState.mState = ButtonState.States.Move;
                }
                
                //InstantMove(PostionFont, PostionNow, movewidth, 200);
            }
        }

        private void PostionBG_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (ButtonState.mState == ButtonState.States.Move && mDoubleAnimtion)
            {
                ButtonState.mState = ButtonState.States.Up;

                mStory.Stop();
                double movewidth = e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(165)   Convert.ToDouble(100) / mVideoLong * PostionBG.Width;
                MainMedia.Position = TimeSpan.FromMilliseconds(mVideoLong * movewidth / PostionBG.Width);
                mTime = mVideoLong - mVideoLong * (e.GetPosition(ButtomCanvas).X - GlobalManager.TransValueFromWidth(165)) / PostionBG.Width - 100;
                if (mstateVideo == stateVideo.play)
                {
                    MainMedia.Play();
                    Move(PostionFont, PostionNow);

                }
                else
                {
                    mBoolMove = true;
                }
                ButtonState.mState = ButtonState.States.None;
            }
            else if (ButtonState.mState == ButtonState.States.Down)
            {
                ButtonState.mState = ButtonState.States.Up;
                ButtonState.mState = ButtonState.States.None;
            }
        }

        private void TJ_MouseDown(object sender, MouseButtonEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "视频文件(*.avi,*.mp4,*.wmv)|*.avi;*.mp4;*.wmv";
            ofd.Title = "打开视频";
            ofd.AddExtension = true;
            ofd.ValidateNames = true;
            ofd.CheckPathExists = true;
            ofd.CheckFileExists = true;

            if (ofd.ShowDialog().ToString() == "True")
            {
                SetPlay(ofd.FileName);
                //Console.WriteLine(ofd.FileName);
            }
            else
            {
                Console.WriteLine(ofd.ShowDialog().ToString());
            }
        }
    }
}

标签: C# 视频 播放

实例下载地址

C# 播放mp4/avi/wmv视频文件示例源码(wpf)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
;
报警
var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_4847253'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s13.cnzz.com/stat.php%3Fid%3D4847253' type='text/javascript'%3E%3C/script%3E"));