如何在winform程序中播放音频和视频?其实方法有不少,我们要做的就是找到一个最好的。最简单的是用系统自带的WindowsMediaPlayer组件(值得欣慰的是,Win10系统仍然可以找到,找不到就要安装插件),播放音频没问题了,但播放视频够呛,支持的视频格式太少了;还可以使用暴风影音的插件(Win7可以,但Win10不一定能用),效果还可以。其实还有更简单的方法,下面逐个讲解
1、系统的WindowsMediaPlayer插件
引用完用下面代码就可以实现播放音频,视频的话如果不支持,它就直接播放音频
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();
看看播放音频的效果
2、暴风影音插件
以前项目在Win7运行正常,换到Win10系统,再打开页面提示。看来是需要注册,但在COM面板里一直没找到,估计是暴风换新组件,不支持Win10了。

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();
}
}
看下播放视频的效果
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的相关参数。这里也有个小技巧,用cmd命令行找到ffplay的所在文件夹,然后键入:ffplay -help >help.txt,就能将命令参数保存成txt文件




文章评论