1. 下载direcly-show lib DLL
点击打开链接

2. DxPlay.cs (能够在下载的样例中找到):

  

 public class DxPlay : IDisposable
{
enum GraphState
{
Stopped,
Paused,
Running,
Exiting
} #region Member variables // File name we are playing
private string m_sFileName; // graph builder interfaces
private IFilterGraph2 m_FilterGraph;
private IMediaControl m_mediaCtrl;
private IMediaEvent m_mediaEvent; // Used to grab current snapshots
ISampleGrabber m_sampGrabber = null; // Grab once. Used to create bitmap
private int m_videoWidth;
private int m_videoHeight;
private int m_stride;
private int m_ImageSize; // In bytes // Event used by Media Event thread
private ManualResetEvent m_mre; // Current state of the graph (can change async)
volatile private GraphState m_State = GraphState.Stopped; #if DEBUG
// Allow you to "Connect to remote graph" from GraphEdit
DsROTEntry m_DsRot;
#endif #endregion // Release everything.
public void Dispose()
{
CloseInterfaces();
}
~DxPlay()
{
CloseInterfaces();
} // Event that is called when a clip finishs playing
public event DxPlayEvent StopPlay;
public delegate void DxPlayEvent(Object sender); // Play an avi file into a window. Allow for snapshots.
// (Control to show video in, Avi file to play
public DxPlay(Control hWin, string FileName)
{
try
{
int hr;
IntPtr hEvent; // Save off the file name
m_sFileName = FileName; // Set up the graph
SetupGraph(hWin, FileName); // Get the event handle the graph will use to signal
// when events occur
hr = m_mediaEvent.GetEventHandle(out hEvent);
DsError.ThrowExceptionForHR(hr); // Wrap the graph event with a ManualResetEvent
m_mre = new ManualResetEvent(false);
#if USING_NET11
m_mre.Handle = hEvent;
#else
m_mre.SafeWaitHandle = new Microsoft.Win32.SafeHandles.SafeWaitHandle(hEvent, true);
#endif // Create a new thread to wait for events
Thread t = new Thread(new ThreadStart(this.EventWait));
t.Name = "Media Event Thread";
t.Start();
}
catch
{
Dispose();
throw;
}
} // Wait for events to happen. This approach uses waiting on an event handle.
// The nice thing about doing it this way is that you aren't in the windows message
// loop, and don't have to worry about re-entrency or taking too long. Plus, being
// in a class as we are, we don't have access to the message loop.
// Alternately, you can receive your events as windows messages. See
// IMediaEventEx.SetNotifyWindow.
private void EventWait()
{
// Returned when GetEvent is called but there are no events
const int E_ABORT = unchecked((int)0x80004004); int hr;
IntPtr p1, p2;
EventCode ec; do
{
// Wait for an event
m_mre.WaitOne(-1, true); // Avoid contention for m_State
lock (this)
{
// If we are not shutting down
if (m_State != GraphState.Exiting)
{
// Read the event
for (
hr = m_mediaEvent.GetEvent(out ec, out p1, out p2, 0);
hr >= 0;
hr = m_mediaEvent.GetEvent(out ec, out p1, out p2, 0)
)
{
// Write the event name to the debug window
Debug.WriteLine(ec.ToString()); // If the clip is finished playing
if (ec == EventCode.Complete)
{
// Call Stop() to set state
Stop(); // Throw event
if (StopPlay != null)
{
StopPlay(this);
}
} // Release any resources the message allocated
hr = m_mediaEvent.FreeEventParams(ec, p1, p2);
DsError.ThrowExceptionForHR(hr);
} // If the error that exited the loop wasn't due to running out of events
if (hr != E_ABORT)
{
DsError.ThrowExceptionForHR(hr);
}
}
else
{
// We are shutting down
break;
}
}
} while (true);
} // Return the currently playing file name
public string FileName
{
get
{
return m_sFileName;
}
} // start playing
public void Start()
{
// If we aren't already playing (or shutting down)
if (m_State == GraphState.Stopped || m_State == GraphState.Paused)
{
int hr = m_mediaCtrl.Run();
DsError.ThrowExceptionForHR(hr); m_State = GraphState.Running;
}
} // Pause the capture graph.
public void Pause()
{
// If we are playing
if (m_State == GraphState.Running)
{
int hr = m_mediaCtrl.Pause();
DsError.ThrowExceptionForHR(hr); m_State = GraphState.Paused;
}
}
// Pause the capture graph.
public void Stop()
{
// Can only Stop when playing or paused
if (m_State == GraphState.Running || m_State == GraphState.Paused)
{
int hr = m_mediaCtrl.Stop();
DsError.ThrowExceptionForHR(hr); m_State = GraphState.Stopped;
}
} // Reset the clip back to the beginning
public void Rewind()
{
int hr; IMediaPosition imp = m_FilterGraph as IMediaPosition;
hr = imp.put_CurrentPosition(0);
} // Grab a snapshot of the most recent image played.
// Returns A pointer to the raw pixel data. Caller must release this memory with
// Marshal.FreeCoTaskMem when it is no longer needed.
public IntPtr SnapShot()
{
int hr;
IntPtr ip = IntPtr.Zero;
int iBuffSize = 0; // Read the buffer size
hr = m_sampGrabber.GetCurrentBuffer(ref iBuffSize, ip);
DsError.ThrowExceptionForHR(hr); Debug.Assert(iBuffSize == m_ImageSize, "Unexpected buffer size"); // Allocate the buffer and read it
ip = Marshal.AllocCoTaskMem(iBuffSize); hr = m_sampGrabber.GetCurrentBuffer(ref iBuffSize, ip);
DsError.ThrowExceptionForHR(hr); return ip;
} // Convert a point to the raw pixel data to a .NET bitmap
public Bitmap IPToBmp(IntPtr ip)
{
// We know the Bits Per Pixel is 24 (3 bytes) because we forced it
// to be with sampGrabber.SetMediaType()
int iBufSize = m_videoWidth * m_videoHeight * 3; return new Bitmap(
m_videoWidth,
m_videoHeight,
-m_stride,
PixelFormat.Format24bppRgb,
(IntPtr)(ip.ToInt32() + iBufSize - m_stride)
);
} // Build the capture graph for grabber and renderer.</summary>
// (Control to show video in, Filename to play)
private void SetupGraph(Control hWin, string FileName)
{
int hr; // Get the graphbuilder object
m_FilterGraph = new FilterGraph() as IFilterGraph2; // Get a ICaptureGraphBuilder2 to help build the graph
ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; try
{
// Link the ICaptureGraphBuilder2 to the IFilterGraph2
hr = icgb2.SetFiltergraph(m_FilterGraph);
DsError.ThrowExceptionForHR(hr); #if DEBUG
// Allows you to view the graph with GraphEdit File/Connect
m_DsRot = new DsROTEntry(m_FilterGraph);
#endif
// Add the filters necessary to render the file. This function will
// work with a number of different file types.
IBaseFilter sourceFilter = null;
hr = m_FilterGraph.AddSourceFilter(FileName, FileName, out sourceFilter);
DsError.ThrowExceptionForHR(hr); // Get the SampleGrabber interface
m_sampGrabber = (ISampleGrabber)new SampleGrabber();
IBaseFilter baseGrabFlt = (IBaseFilter)m_sampGrabber; // Configure the Sample Grabber
ConfigureSampleGrabber(m_sampGrabber); // Add it to the filter
hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber");
DsError.ThrowExceptionForHR(hr); // Connect the pieces together, use the default renderer
hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, null);
DsError.ThrowExceptionForHR(hr); // Now that the graph is built, read the dimensions of the bitmaps we'll be getting
SaveSizeInfo(m_sampGrabber); // Configure the Video Window
IVideoWindow videoWindow = m_FilterGraph as IVideoWindow;
ConfigureVideoWindow(videoWindow, hWin); // Grab some other interfaces
m_mediaEvent = m_FilterGraph as IMediaEvent;
m_mediaCtrl = m_FilterGraph as IMediaControl;
}
finally
{
if (icgb2 != null)
{
Marshal.ReleaseComObject(icgb2);
icgb2 = null;
}
}
#if DEBUG
// Double check to make sure we aren't releasing something
// important.
GC.Collect();
GC.WaitForPendingFinalizers();
#endif
} // Configure the video window
private void ConfigureVideoWindow(IVideoWindow videoWindow, Control hWin)
{
int hr; // Set the output window
hr = videoWindow.put_Owner(hWin.Handle);
DsError.ThrowExceptionForHR(hr); // Set the window style
hr = videoWindow.put_WindowStyle((WindowStyle.Child | WindowStyle.ClipChildren | WindowStyle.ClipSiblings));
DsError.ThrowExceptionForHR(hr); // Make the window visible
hr = videoWindow.put_Visible(OABool.True);
DsError.ThrowExceptionForHR(hr); // Position the playing location
Rectangle rc = hWin.ClientRectangle;
hr = videoWindow.SetWindowPosition(0, 0, rc.Right, rc.Bottom);
DsError.ThrowExceptionForHR(hr);
} // Set the options on the sample grabber
private void ConfigureSampleGrabber(ISampleGrabber sampGrabber)
{
AMMediaType media;
int hr; // Set the media type to Video/RBG24
media = new AMMediaType();
media.majorType = MediaType.Video;
media.subType = MediaSubType.RGB24;
media.formatType = FormatType.VideoInfo;
hr = sampGrabber.SetMediaType(media);
DsError.ThrowExceptionForHR(hr); DsUtils.FreeAMMediaType(media);
media = null; // Configure the samplegrabber
hr = sampGrabber.SetBufferSamples(true);
DsError.ThrowExceptionForHR(hr);
} // Save the size parameters for use in SnapShot
private void SaveSizeInfo(ISampleGrabber sampGrabber)
{
int hr; // Get the media type from the SampleGrabber
AMMediaType media = new AMMediaType(); hr = sampGrabber.GetConnectedMediaType(media);
DsError.ThrowExceptionForHR(hr); try
{ if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
{
throw new NotSupportedException("Unknown Grabber Media Format");
} // Get the struct
VideoInfoHeader videoInfoHeader = new VideoInfoHeader();
Marshal.PtrToStructure(media.formatPtr, videoInfoHeader); // Grab the size info
m_videoWidth = videoInfoHeader.BmiHeader.Width;
m_videoHeight = videoInfoHeader.BmiHeader.Height;
m_stride = videoInfoHeader.BmiHeader.ImageSize / m_videoHeight;
m_ImageSize = videoInfoHeader.BmiHeader.ImageSize;
}
finally
{
DsUtils.FreeAMMediaType(media);
media = null;
}
} // Shut down capture
private void CloseInterfaces()
{
int hr; lock (this)
{
if (m_State != GraphState.Exiting)
{
m_State = GraphState.Exiting; // Release the thread (if the thread was started)
if (m_mre != null)
{
m_mre.Set();
}
} if (m_mediaCtrl != null)
{
// Stop the graph
hr = m_mediaCtrl.Stop();
m_mediaCtrl = null;
} if (m_sampGrabber != null)
{
Marshal.ReleaseComObject(m_sampGrabber);
m_sampGrabber = null;
} #if DEBUG
if (m_DsRot != null)
{
m_DsRot.Dispose();
}
#endif if (m_FilterGraph != null)
{
Marshal.ReleaseComObject(m_FilterGraph);
m_FilterGraph = null;
}
}
GC.Collect();
}
}

3. 将Player载入到指定的panel中,本例须要循环播放。因此在stopplay中自己主动start()

private DxPlay m_play;
private void LoadVideoIntoPanel()
{
try
{
var pathBase = Path.GetDirectoryName(Application.ExecutablePath);
var path = Path.Combine(pathBase, bgVideo);
// Open the file, provide a handle to play it in
m_play = new DxPlay(_videoBackground, path);
m_play.Start(); // Let us know when the file is finished playing
m_play.StopPlay += sender =>
{
// Rewind clip to beginning to allow DxPlay.Start to work again.
m_play.Rewind();
m_play.Start();
}; }
catch (Exception ex)
{
_log.Error("Video Playing Error ==> " + ex.Message + "\r\n" + ex.StackTrace);
}
}

C# Windows form application 播放小视频的更多相关文章

  1. 微信电脑版微信1.1 for Windows更新 可@人/转发撤回消息/可播小视频

    微信电脑版微信1.1 for Windows发布更新了,版本号为1.1.0.18,群聊可@人/可转发撤回消息/可播小视频,功能越来越接近微信手机版了. 本次更新的一些新特点: 群聊中可以@人. 消息可 ...

  2. 与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成

    原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成 [索引页][源码下载] 与众不同 win ...

  3. Windows Phone 7 播放视频

    在Windows Phone 7中播放视频有两种方式,一种是使用MediaElement 控件来播放,一种是使用启动器MediaPlayerLanucher来实现视频的播放.用MediaElement ...

  4. Windows Phone 之播放视频

    在Windows Phone 7中播放视频有两种方式, (1)使用MediaElement 控件来播放:用MediaElement 控件来播放视频比较灵活,你需要自己去实现播放暂停进度条等等的功能,播 ...

  5. Python爬取抖音高颜值小视频

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 有趣的python PS:如有需要Python学习资料的小伙伴可以加 ...

  6. IIS中asp网站播放flv视频技术

    播放flv视频文件需要使用flvplayer.swf程序(32K). HTML嵌入代码: <div id="FlashFile"> <object type=&q ...

  7. VLC播放RTSP视频延迟问题 (转)

    原帖地址:http://blog.chinaunix.net/uid-26611383-id-3755283.html ======================================== ...

  8. 前端Web浏览器基于Flash如何实时播放监控视频画面(前言)之流程介绍

    [关键字:前端浏览器如何播放RTSP流画面.前端浏览器如何播放RTMP流画面] 本片文章只是起到抛砖引玉的作用,能从头到尾走通就行,并不做深入研究.为了让文章通俗易懂,尽量使用白话描述. 考虑到视频延 ...

  9. Vue3.0短视频+直播|vue3+vite2+vant3仿抖音界面|vue3.x小视频实例

    基于vue3.0构建移动端仿抖音/快手短视频+直播实战项目Vue3-DouYin. 5G时代已来,短视频也越来越成为新一代年轻人的娱乐方式,在这个特殊之年,又将再一次成为新年俗! 基于vue3.x+v ...

随机推荐

  1. docker从零开始网络(四 ) host网络

    使用主机网络 如果host对容器使用网络驱动程序,则该容器的网络堆栈不会与Docker主机隔离.例如,如果您运行绑定到端口80 host的容器并使用网络,则容器的应用程序将在主机IP地址的端口80上可 ...

  2. hdu 1498(最小点覆盖集)

    50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. ajaxfileupload异步上传文件

    ajaxfileupload插件可以以异步方式上传文件(其实现:iframe),不像传统那样需要刷新,下面就介绍下其使用 1.HTML部分(先引入jquery): <!DOCTYPE html& ...

  4. JavaWEB开发框架:Shiro

    通过了三个月的实习,在javaWEB开发的过程当中,学习到了一些新的知识,特此记录一下学习到的一种新的WEB开发当中常用的用户认证和授权的安全框架,Shiro. 首先,要先知道shiro这个框架主要在 ...

  5. STL模板整理 map

    map容器: 继上一篇 ,Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能 ...

  6. 如何在Android Studio中创建jniLib和asset文件夹 2

    1.创建asset文件夹 如图进行操作 2.创建jniLib文件夹 —打开app下面的gradle文件(不是project的gradle) —在gradle文件的Android标签里面添加 sourc ...

  7. 51nod 1265 四点共面【计算几何+线性代数】

    1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点是否在同一个平面内(4点共 ...

  8. CSU七月校赛B

    #include <iostream> #include<cstdio> #include<algorithm> #include<cstring> # ...

  9. 【枚举】Codeforces Round #394 (Div. 2) C. Dasha and Password

    纪念死去的智商(虽然本来就没有吧……) 三重循环枚举将哪三个fix string作为数字.字母和符号位.记下最小的值就行了. 预处理之后这个做法应该是O(n^3)的,当然完全足够.不预处理是O(n^3 ...

  10. 动态NAT地址转换

    1.配置路由器的端口ip地址(注意外网和内网ip地址的设置) Router(config)#inter f0/0 Router(config-if)#ip add 192.168.1.1 255.25 ...