小Q的博客

  • 首页
  • net编程
    • 产品和框架
    • 开发实例
    • 经验技巧
    • 开源组件
  • wp独立站
  • 自媒体
  • 日记本
  • 工具箱
每个程序员,都应该有一个自己的博客站
  1. 首页
  2. net编程
  3. 经验技巧
  4. 正文

winform中播放视频?ffplay比MediaPlayer和暴风都好用

2022年11月9日 750点热度 1人点赞 0条评论

如何在winform程序中播放音频和视频?其实方法有不少,我们要做的就是找到一个最好的。最简单的是用系统自带的WindowsMediaPlayer组件(值得欣慰的是,Win10系统仍然可以找到,找不到就要安装插件),播放音频没问题了,但播放视频够呛,支持的视频格式太少了;还可以使用暴风影音的插件(Win7可以,但Win10不一定能用),效果还可以。其实还有更简单的方法,下面逐个讲解

目录

  • 1、系统的WindowsMediaPlayer插件
  • 2、暴风影音插件
  • 3、VLC组件
  • 4、ffplay组件

1、系统的WindowsMediaPlayer插件

引用的话,要到COM组件里,就是我选中的这个。
1.引用MediaPlayer的COM组件

 

引用完用下面代码就可以实现播放音频,视频的话如果不支持,它就直接播放音频

this.mp3Player = new AxWMPLib.AxWindowsMediaPlayer();
this.mp3Player.Dock = DockStyle.Fill;
this.mp3Player.Show();

this.Controls.Clear();
this.Controls.Add(this.mp3Player);

//播放Flash文件.判断文件是否存在?
if (!System.IO.File.Exists(fileFullName)) 
    throw new System.IO.FileNotFoundException(fileFullName + "文件未找到");

IWMPMedia media = this.mp3Player.newMedia(fileFullName);
this.mp3Player.currentPlaylist.clear();
this.mp3Player.currentPlaylist.appendItem(media);
this.mp3Player.Ctlcontrols.play();

看看播放音频的效果

3.MediaPlayer的播放效果

 

2、暴风影音插件

以前项目在Win7运行正常,换到Win10系统,再打开页面提示。看来是需要注册,但在COM面板里一直没找到,估计是暴风换新组件,不支持Win10了。
4.引用暴风COM组件的代码

 

3、VLC组件

网上找相关资料的时候,找到了这个项目,能播放视频。代码别人封装好的,直接拿来用就可以。效果还不错,如果想细化下,就要知道里面的参数才可以。注:需要引入一个VlcPlayer的类,有需要的可以加小编微信:xiyang1011,免费获取

public partial class FormVLCPlayerDemo : Form
    {
        private string tmpFile = null;
        private VlcPlayer vlc_Rtsp = null;

        public FormVLCPlayerDemo()
        {
            InitializeComponent();

            this.VLCInit();
        }

        public void VLCInit()
        {
            string pluginPath = System.Environment.CurrentDirectory + "\\plugins\\";
            this.vlc_Rtsp = new VlcPlayer(pluginPath, true);

            IntPtr render_wnd = this.panel1.Handle;
            this.vlc_Rtsp.SetRenderWindow((int)render_wnd);
        }

        private void Play(string fileName)
        {
            vlc_Rtsp.PlayFile(fileName);

        }

        private void Stop()
        {
            vlc_Rtsp.Stop();
        }

        private void btnOpenFileAndPlay_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "加载视频";
            openFileDialog.Filter = "视频文件|*.mp4|所有文件|*.*";
            openFileDialog.FileName = string.Empty;
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.DefaultExt = ".mp4";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileText.Text = openFileDialog.FileName;

                if (File.Exists(FileText.Text))
                {
                    tmpFile = FileText.Text;

                    try
                    {
                        if (tmpFile != string.Empty)
                        {
                            this.Play(tmpFile);
                        }
                    }
                    catch
                    { }

                    tmpFile = string.Empty;
                }
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            this.Stop();
        }
    }

 

看下播放视频的效果

vlc组件播放效果

4、ffplay组件

这个就是我们今天要重要讲的。简单说,它是ffmpeg的兄弟,都在同一文件夹里。我们一直用ffmpeg做音频编辑,却忽略了ffplay这个。下面代码就能播放视频(也能播放音频)。但它打开的是一个命令行窗体,我们要做的就是把它嵌入到winform的页面里(代码的最后2行)。如果能实现,就是非常very nice的。当然,这是能实现的

先实现用ffplay播放一个视频
然后再调用系统API,找到句柄,再把这个窗体移动到Winform的程序里。代码如下

private void PlayerAudioOrMedaiByFFPlay(string fileName, string args)
{
    this.ffplayProcess.StartInfo.FileName = fileName;
    this.ffplayProcess.StartInfo.Arguments = args + " -noborder";

    this.ffplayProcess.StartInfo.CreateNoWindow = true;
    this.ffplayProcess.StartInfo.RedirectStandardOutput = true;
    this.ffplayProcess.StartInfo.UseShellExecute = false;

    this.ffplayProcess.EnableRaisingEvents = true;
    this.ffplayProcess.OutputDataReceived += (o, e) => Debug.WriteLine(e.Data ?? "NULL", "ffplay");
    this.ffplayProcess.ErrorDataReceived += (o, e) => Debug.WriteLine(e.Data ?? "NULL", "ffplay");
    this.ffplayProcess.Exited += (o, e) => Debug.WriteLine("Exited", "ffplay");
    this.ffplayProcess.Start();

    Thread.Sleep(500);

    SetParent(this.ffplayProcess.MainWindowHandle, this.panelControl1.Handle);

    MoveWindow(this.ffplayProcess.MainWindowHandle, 0, 0, 960, 448, true);
}

打开个视频文件看看

//视频文件
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "加载视频";
openFileDialog.Filter = "视频文件|*.mp4|所有文件|*.*";
openFileDialog.FileName = string.Empty;
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
openFileDialog.DefaultExt = ".mp4";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    string args = @"""" + openFileDialog.FileName + @"""";
    this.PlayerAudioOrMedaiByFFPlay(this.ffplayFile, args);
}

 

看下播放视频文件的效果ffplay播放效果

 

看效果还不错吧,简单的播放个视频已经没问题了。如果想对视频实现可视化编辑,就需要进一步的操作。起码要了解ffplay的相关参数。这里也有个小技巧,用cmd命令行找到ffplay的所在文件夹,然后键入:ffplay -help >help.txt,就能将命令参数保存成txt文件

标签: ffplay vlcplayer windowsmediaplayer winform 播放视频 播放音频
最后更新:2022年11月9日

小Q

80后中年不油腻大叔,喜欢编写代码、打羽毛球、做木制玩具。目前定居浙江杭州

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022 小Q的博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙ICP备2022019157号-2