/*C#.NET2005实现一个流媒体播放器
2007-04-05 16:38
用C#.NET2005实现一个流媒体播放器,感觉还是不错的,就是调用了Windows中的一个动态连接库——Quarz.dll。我写的是一个DirectShow多媒体播放器。支持目前为止的大部分格式。代码共享一下:*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuartzTypeLib;//这个就是Quarz.dll转换后的Dll文件
namespace myMedia
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// private System.ComponentModel.IContainer components;
private const int WM_APP = 0x8000;
private const int WM_GRAPHNOTIEY = WM_APP + 1;
private const int EC_COMPLETE = 0x01;
private const int WS_CHILD = 0x40000000;
private const int WS_CLIPCHILDREN = 0x2000000;
private FilgraphManager m_objFilterGraph = null;
private IBasicAudio m_objBasicAudio = null;
private IVideoWindow m_objVideoWindow = null;
private IMediaEvent m_objMediaEvent = null;
private IMediaEventEx m_objMediaEventEx = null;
private IMediaPosition m_objMediaPosition = null;
private IMediaControl m_objMediaControl = null;
enum MadiaStatus { None, Stopped, Paused, Running };
private MadiaStatus m_CurrentStatus = MadiaStatus.None;
private void CleanUp()
{
if (m_objMediaControl != null)
{
m_objMediaControl.Stop();
}
m_CurrentStatus = MadiaStatus.Stopped;
if (m_objMediaEventEx != null)
m_objMediaEventEx.SetNotifyWindow(0, 0, 0);
if (m_objVideoWindow != null)
{
m_objVideoWindow.Visible = 0;
m_objVideoWindow.Owner = 0;
}
if (m_objMediaControl != null) m_objMediaControl = null;
if (m_objMediaPosition != null) m_objMediaPosition = null;
if (m_objMediaEventEx != null) m_objMediaEventEx = null;
if (m_objMediaEvent != null) m_objMediaEvent = null;
if (m_objBasicAudio != null) m_objBasicAudio = null;
if (m_objFilterGraph != null) m_objFilterGraph = null;
}
private void Open_Dialog(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Media Files|*.mpg;*.avi;*.wma;*.mov;*.mav;*.mp2;*.mp3;*.rm;*.rmvb|All Files|*.*";
if (DialogResult.OK == openFileDialog.ShowDialog())
{
CleanUp();
m_objFilterGraph = new FilgraphManager();
m_objFilterGraph.RenderFile(openFileDialog.FileName);
try
{
m_objBasicAudio = m_objFilterGraph as IBasicAudio;
m_objVideoWindow = m_objFilterGraph as IVideoWindow;
m_objVideoWindow.Owner = (int)panel1.Handle;
m_objVideoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN;
m_objVideoWindow.SetWindowPosition(panel1.ClientRectangle.Left, panel1.ClientRectangle.Top, panel1.ClientRectangle.Width, panel1.ClientRectangle.Height);
}
catch (Exception ex)
{
MessageBox.Show("该文件没有图象信息!");
m_objVideoWindow = null;
}
m_objMediaEvent = m_objFilterGraph as IMediaEvent;
m_objMediaEventEx = m_objFilterGraph as IMediaEventEx;
m_objMediaPosition = m_objFilterGraph as IMediaPosition;
m_objMediaControl = m_objFilterGraph as IMediaControl;
this.Text = "DirectShow - [" + openFileDialog.FileName + "]";
m_objMediaControl.Run();
m_CurrentStatus = MadiaStatus.Running;
}
}
private void UpdateToolBar()//用了一个toolStrip
{
switch (m_CurrentStatus)
{
case MadiaStatus.None:
toolStripButton1.Enabled = true;
toolStripButton2.Enabled = false;
toolStripButton3.Enabled = false;
toolStripButton4.Enabled = false;
toolStripButton5.Enabled = true;
break;
case MadiaStatus.Paused:
toolStripButton1.Enabled = false;
toolStripButton2.Enabled = true;
toolStripButton3.Enabled = false;
toolStripButton4.Enabled = true;
toolStripButton5.Enabled = true;
break;
case MadiaStatus.Running:
toolStripButton1.Enabled = false;
toolStripButton2.Enabled = false;
toolStripButton3.Enabled = true;
toolStripButton4.Enabled = true;
toolStripButton5.Enabled = true;
break;
case MadiaStatus.Stopped:
toolStripButton1.Enabled = true;
toolStripButton2.Enabled = false;
toolStripButton3.Enabled = false;
toolStripButton4.Enabled = false;
toolStripButton5.Enabled = true;
break;
}
}
private void ExitWin(object sender, System.EventArgs e)
{
this.Close();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Open_Dialog(sender, e);
UpdateToolBar();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
m_objMediaControl.Run();
m_CurrentStatus = MadiaStatus.Running;
UpdateToolBar();
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
m_objMediaControl.Pause();
m_CurrentStatus = MadiaStatus.Paused;
UpdateToolBar();
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
m_objMediaPosition.CurrentPosition = 0;
m_CurrentStatus = MadiaStatus.Stopped;
CleanUp();
UpdateToolBar();
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
ExitWin(sender, e);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
panel1.Size = Form1.ActiveForm.Size;
if (m_objVideoWindow != null)
{
m_objVideoWindow.SetWindowPosition(panel1.ClientRectangle.Left, panel1.ClientRectangle.Top, panel1.ClientRectangle.Width, panel1.ClientRectangle.Height);
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "我的制作——流媒体播放器";
UpdateToolBar();
}
}
C#.NET2005实现一个流媒体播放器
最新推荐文章于 2024-11-06 11:34:16 发布