我在Visual Studio中编写C#app,使用DirectShow(Quartz.dll)播放视频。我遇到的一个问题是,当用户点击停止按钮停止视频播放时,视频会回到开头。另一个问题是从picturbox控件中清除视频的最后一帧。
以下是与QuartzTypeLib相关的顶部代码:
public partial class frmMain : Form
{
public const int WS_CHILD = 0x40000000;
public const int WS_CLIPCHILDREN = 0x2000000;
public QuartzTypeLib.IMediaControl mc;
public QuartzTypeLib.IVideoWindow videoWindow = null;
这是打开文件的代码,包括用于创建FileGraphManager实例的代码:
private void openMediaToolStripMenuItem_Click(object sender, EventArgs e)
{
// Open a media file.
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Video Files|*.mpg;*.avi;*;*.wmv;*.mov|Audio Files|*.mp3;*.wav;*.wma";
ofd.FilterIndex = 1;
if (DialogResult.OK == ofd.ShowDialog())
{
// Stop the playback for the current video if one is currently playing.
if (mc != null)
mc.Stop();
if (pbVideoDisplay.Image != null)
pbVideoDisplay.Image = null;
// Load the movie file.
FilgraphManager graphManager = new FilgraphManager();
graphManager.RenderFile(ofd.FileName);
mc = (IMediaControl)graphManager;
// Send the video to the picture box control pbVideoDisplay) on frmMain.
try
{
videoWindow = (IVideoWindow)graphManager;
videoWindow.Owner = (int)pbVideoDisplay.Handle;
videoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN;
videoWindow.SetWindowPosition(
pbVideoDisplay.ClientRectangle.Left,
pbVideoDisplay.ClientRectangle.Top,
pbVideoDisplay.ClientRectangle.Width,
pbVideoDisplay.ClientRectangle.Height);
}
catch
{
}
}
}
以下是关闭媒体的代码:
private void closeMediaToolStripMenuItem_Click(object sender, EventArgs e)
{
mc.Stop();
}
以下是停止视频播放的代码:
private void tsbtnStop_Click(object sender, EventArgs e)
{
mc.Stop();
}
mc.Stop()会在视频的当前位置停止视频,就像mc.Pause()函数一样。当调用mc.Stop()时,如何将视频位置重置为视频的开头?如何清除图片框控件中的最后一个视频帧?我试过“pbVideoDisplay.Image = null;”但最后一个视频帧仍然存在。显然我需要更多代码来关闭视频,但我无法在任何地方找到任何样本。
答案 0 :(得分:0)
videoWindow.put_Visible
显示或隐藏视频窗口。(mc as IMediaPosition).put_CurrentPosition
设置当前位置,相对于流的总持续时间。答案 1 :(得分:0)
您需要添加StopWhenReady()方法并设置属性CurrentPosition =0。以下过程:
private void tsbtnStop_Click(object sender, EventArgs e)
{
mc.Stop();
mc.StopWhenReady();
IMediaPosition mcPos;
mcPos = (IMediaPosition)mc;
mcPos.CurrentPosition = 0;
}