bandariiswear

幽艳纷相竞 秾纤各自奇 虽然春烂漫 也要笔扶持 清人题画诗

导航

CS中调用微软自带com组件实现音频视频播放(wf/wpf)

1.mp3播放器:工具箱中右键,在弹出的菜单中选择“选择项”,添加“com组件”,选择名称“windows Media Player",点击确定就会在工具箱新增一个“windows Media Player"控件。程序中隐藏到了wf本身的边框,用picturebox实现移动无边框窗体效果,通过定时器实时更新进度条,最后理解控件playState的几种状态就没有什么了。

  #region 移动无边框窗体
        private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
        {
            int xOffset;
            int yOffset;
            if (e.Button == MouseButtons.Left)
            {
                xOffset = -e.X;
                yOffset = -e.Y;
                mouseOffset = new Point(xOffset, yOffset);
                isMouseDown = true;
            }
        }

        private void pictureBox3_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouseOffset.X, mouseOffset.Y);
                Location = mousePos;
            }
        }

        private void pictureBox3_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = false;
            }
        }
        #endregion
   int m = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            int i = (int)axWindowsMediaPlayer1.playState;
            switch (i)
            {
                case 1: lblStauts.Text = "状态:停止"; break;
                case 2: lblStauts.Text = "状态:暂停"; break;
                case 3: lblStauts.Text = "状态:播放"; break;
                case 6: lblStauts.Text = "状态:正在缓冲"; break;
                case 9: lblStauts.Text = "状态:正在连接"; break;
                case 10: lblStauts.Text = "状态:准备就绪"; break;
            }
            lbljindu.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString;
            if (m == 1)
            {
                hScrollBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
                hScrollBar1.Minimum = 0;
                hScrollBar1.Value = (int)axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
                hScrollBar2.Value = axWindowsMediaPlayer1.settings.volume;
            }
        }
View Code

 mp3player

2.wpf音频视频播放器

 本视频播放器主要用到MediaElement控件(微软官网例子),它是wpf中提供的一个包含音频和视频的控件,位于System.Windows.Controls下,方法同第一个mp3播放器的类似,不过由于wpf中没有”打开“对话框的相关类OpenFileDialog,所以需要添加wf窗体应用程序中基础类库的引用(右键”添加引用"----".Net"----"System.Windows.Forms")

用于处理全屏的类:

 public static class AllScreen
    {
        private static Window _fullWindow;
        private static WindowState _windowState;
        private static WindowStyle _windowStyle;
        private static bool _windowTopMost;
        private static ResizeMode _windowResizeMode;
        private static Rect _windowRect;

        /// <summary>
        /// 进入全屏
        /// </summary>
        /// <param name="window"></param>
        public static void GoFullscreen(this Window window)
        {
            //存储窗体信息
            _windowState = window.WindowState;
            _windowStyle = window.WindowStyle;
            _windowTopMost = window.Topmost;
            _windowResizeMode = window.ResizeMode;
            _windowRect.X = window.Left;
            _windowRect.Y = window.Top;
            _windowRect.Width = window.Width;
            _windowRect.Height = window.Height;
            //变成无边窗体
            window.WindowState = WindowState.Normal;//假如已经是Maximized,就不能进入全屏,所以这里先调整状态
            window.WindowStyle = WindowStyle.None;
            window.ResizeMode = ResizeMode.NoResize;
            window.Topmost = true;//最大化后总是在最上面
            //获取窗口句柄 
            var handle = new WindowInteropHelper(window).Handle;
            //获取当前显示器屏幕
            Screen screen = Screen.FromHandle(handle);
            //调整窗口最大化,全屏的关键代码就是下面3句
            window.MaxWidth = screen.Bounds.Width;
            window.MaxHeight = screen.Bounds.Height;
            window.WindowState = WindowState.Maximized;
            //解决切换应用程序的问题
            window.Activated += new EventHandler(window_Activated);
            window.Deactivated += new EventHandler(window_Deactivated);
            //记住成功最大化的窗体
            _fullWindow = window;
        }

        static void window_Deactivated(object sender, EventArgs e)
        {
            var window = sender as Window;
            window.Topmost = false;
        }

        static void window_Activated(object sender, EventArgs e)
        {
            var window = sender as Window;
            window.Topmost = true;
        }

        /// <summary>
        /// 退出全屏
        /// </summary>
        /// <param name="window"></param>
        public static void ExitFullscreen(this Window window)
        {
            //恢复窗口先前信息,这样就退出了全屏
            window.Topmost = _windowTopMost;
            window.WindowStyle = _windowStyle;
            window.ResizeMode = ResizeMode.CanResize;//设置为可调整窗体大小
            window.Left = _windowRect.Left;
            window.Width = _windowRect.Width;
            window.Top = _windowRect.Top;
            window.Height = _windowRect.Height;
            window.WindowState = _windowState;//恢复窗口状态信息
            window.ResizeMode = _windowResizeMode;//恢复窗口可调整信息
            //移除不需要的事件
            window.Activated -= window_Activated;
            window.Deactivated -= window_Deactivated;
            _fullWindow = null;
        }

        /// <summary>
        /// 窗体是否在全屏状态
        /// </summary>
        /// <param name="window"></param>
        /// <returns></returns>
        public static bool IsFullscreen(this Window window)
        {
            if (window == null)
            {
                throw new ArgumentNullException("window");
            }
            return _fullWindow == window;
        }
    }
View Code

3.wpf播放器扩展

这个在窗体上放入3个MediaElement,然后调整一下样式。。。

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            mmBear.Pause();
            mmFractal.Pause();
            mmButterfly.Pause();
        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            mmBear.Play();
            mmFractal.Play();
            mmButterfly.Play();
        }

        private void btnRewind_Click(object sender, RoutedEventArgs e)
        {
            mmBear.Position = new TimeSpan(0);
            mmFractal.Position = new TimeSpan(0);
            mmButterfly.Position = new TimeSpan(0);
        }
View Code

 

3个项目的源代码戳这里或者捅这里

posted on 2013-12-29 19:02  YEKEYISHUO  阅读(2001)  评论(1编辑  收藏  举报