用QuartzTypeLib.dll做过视频的朋友请进来帮忙看一下,winform程序。
用QuartzTypeLib.dll做视频如何获取该视频的时长呢,还有我用ProgressBar做视频的进度条,如何使他与视频同步,当视频缓冲时,他就停止。我是新手。
public partial class mediaplayer : Form { private components; private const int WM_APP = 0x8000; private const int WM_GRAPHNOTIFY = 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 MediaStatus { None, Stopped, Paused, Running }; private MediaStatus m_CurrentStatus = MediaStatus.None; OpenFileDialog openFileDialog; private int second = 0; private int minute = 0; //private int hour = 0; private bool flag = false; public mediaplayer() { InitializeComponent(); this.tsbt_open.Enabled = true; this.tsbt_play.Enabled = false; this.tsbt_stop.Enabled = false; this.tsbt_pause.Enabled = false; } private Boolean Play = false; private void PlayOrPause() { if (m_objMediaControl != null) { if (Play) { m_objMediaControl.Run(); m_CurrentStatus = MediaStatus.Running; this.tsbt_play.Enabled = false; this.tsbt_stop.Enabled = true; this.tsbt_pause.Enabled = true; this.flag = true; Play = !Play; this.toolStripButton1.Image = this.imageList1.Images[2]; } else { m_objMediaControl.Pause(); m_CurrentStatus = MediaStatus.Paused; this.tsbt_play.Enabled = true; this.tsbt_stop.Enabled = true; this.tsbt_pause.Enabled = false; this.flag = false; Play = !Play; this.toolStripButton1.Image = this.imageList1.Images[4]; } } } //播放 private void tsbt_play_Click(object sender, EventArgs e) { PlayOrPause(); } //暂停文件 private void tsbt_pause_Click(object sender, EventArgs e) { PlayOrPause(); } //停止播放 private void tsbt_close_Click(object sender, EventArgs e) { if (m_objMediaControl != null) { m_objMediaControl.Stop(); m_objMediaPosition.CurrentPosition = 0; m_CurrentStatus = MediaStatus.Stopped; CleanUp(); this.tsbt_play.Enabled = false; this.tsbt_stop.Enabled = false; this.tsbt_pause.Enabled = false; this.toolStripButton1.Image = this.imageList1.Images[4]; this.flag = false; this.lb_player2.Text = "播放时间:00:00"; this.trackBar1.Value = 0; //this.Close(); } } private void tsbt_stop_Click(object sender, EventArgs e) { //m_objMediaControl.Stop(); //m_objMediaPosition.CurrentPosition = 0; //m_CurrentStatus = MediaStatus.Stopped; //CleanUp(); //this.tsbt_play.Enabled = false; //this.tsbt_stop.Enabled = false; //this.tsbt_pause.Enabled = false; //this.flag = false; } //关闭 private void CleanUp() { if (m_objMediaControl != null) m_objMediaControl.Stop(); m_CurrentStatus = MediaStatus.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_objVideoWindow != null) m_objVideoWindow = null; if (m_objBasicAudio != null) m_objBasicAudio = null; if (m_objFilterGraph != null) m_objFilterGraph = null; } //清除临时图像 private void tsbt_open_Click(object sender, EventArgs e) { openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Media Files|*.mkv;*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3|All Files|*.*"; if (DialogResult.OK == openFileDialog.ShowDialog()) { CleanUp(); m_objFilterGraph = new FilgraphManager(); try { m_objFilterGraph.RenderFile(openFileDialog.FileName); m_objBasicAudio = m_objFilterGraph as IBasicAudio; m_objVideoWindow = m_objFilterGraph as IVideoWindow; m_objVideoWindow.Owner = (int)pl_play.Handle; m_objVideoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN; m_objVideoWindow.SetWindowPosition(pl_play.ClientRectangle.Left, pl_play.ClientRectangle.Top, pl_play.ClientRectangle.Width, pl_play.ClientRectangle.Height); } catch (Exception ex) { MessageBox.Show("该文件不能视频"); m_objVideoWindow = null; } m_objMediaEvent = m_objFilterGraph as IMediaEvent; m_objMediaEventEx = m_objFilterGraph as IMediaEventEx; m_objMediaEventEx.SetNotifyWindow((int)this.Handle, WM_GRAPHNOTIFY, 0); m_objMediaPosition = m_objFilterGraph as IMediaPosition; m_objMediaControl = m_objFilterGraph as IMediaControl; //播放歌曲名称 this.Text = "DirectShow - [" + openFileDialog.FileName + "]"; //开始播放 m_objMediaControl.Run(); this.toolStripButton1.Image = this.imageList1.Images[2]; waveOutSetVolume(0, (System.UInt32)((double)0xffff * (double)this.tb_player1.Value / (double)(this.tb_player1.Maximum - this.tb_player1.Minimum))); this.trackBar1.Maximum = 160; //设置播放状态 m_CurrentStatus = MediaStatus.Running; this.tsbt_play.Enabled = false; this.tsbt_stop.Enabled = true; this.tsbt_pause.Enabled = true; flag = true; // this.hour = 0; this.second = 0; this.minute = 0; }