MediaPlayer MediaOpenned 事件未在 .net 类上触发
我正在尝试构建一个类,其中我的缩略图类正在捕获帧作为用户上传的视频的缩略图,到目前为止一切进展顺利。我首先在 wpf 应用程序上写下了该应用程序,然后决定转移到我网站上的一个类,名为thumbnail.cs。代码如下所示。
我的问题是, MediaOpenned 事件没有启动,因此执行无法按预期继续,我尝试自己调用它并放置一个 thread.sleep 来处理视频缓冲的延迟,并且代码按我想要的方式工作。但我不想依赖睡眠,因为它可能由于服务器和重而不稳定。我需要它来处理事件或类似的事情。
对于事件未在 .net 上触发的任何想法或建议?在 wpf 上完美工作时?以及该怎么做。
当然还有数以百万计的感谢!
缩略图.cs 类;
MediaPlayer myPlayer = new MediaPlayer();
public thumbnail()
{
myPlayer.Open(new Uri(@"C:\movie.wmv"));
myPlayer.Play();
myPlayer.IsMuted = true;
//this doesn't fire up
myPlayer.MediaOpened += new EventHandler(myPlayer_MediaOpened);
/*
when i do like this it works as i want but unstable and heavy because sleep
myPlayer_MediaOpened(null, null);
System.Threading.Thread.Sleep(5000);
*/
}
void myPlayer_MediaOpened(object sender, EventArgs e)
{
myPlayer.Position = TimeSpan.FromSeconds(myPlayer.NaturalDuration.TimeSpan.Seconds / 2);
RenderTargetBitmap RenderTarBitmap = new RenderTargetBitmap(400, 300, 1 / 96, 1 / 96, PixelFormats.Pbgra32);
DrawingVisual DVisual = new DrawingVisual();
DrawingContext DContext = DVisual.RenderOpen();
DContext.DrawVideo(myPlayer, new Rect(0, 0, 400, 300));
DContext.Close();
RenderTarBitmap.Render(DVisual);
myPlayer.Stop();
BitmapFrame BMPFrame = BitmapFrame.Create(RenderTarBitmap);
BitmapEncoder BMPEncoder = new JpegBitmapEncoder();
System.IO.FileStream FStream = new System.IO.FileStream(@"C:\movie.jpg", System.IO.FileMode.Create);
BMPEncoder.Frames.Add(BMPFrame);
BMPEncoder.Save(FStream);
FStream.Flush();
FStream.Close();
}
--------------------------------------------------------
更新:
好吧,所以我'几天来我一直在努力解决这个问题,天知道帮助我的人会直接去天堂!无论如何,我发现我无法触发与 MediaPlayer 或我的thumbnail.cs(托管在 app_code 中并由 asmx Web 服务调用)相关的任何事件处理程序。
通过使用线程并实现我自己制作的事件处理程序来尝试一些解决方法,检查电影是否已加载。也不走运,MediaPlayer 也不让我在另一个线程中触及它的内部...而且我不想做更多的事情hack 方法,同时能够使用简单的事件处理程序解决问题!
搜索了最深入的谷歌,发现2006年有一个帖子,有人认为这可能是x64的问题(好吧,我正在使用x64),但我在msdn上找不到任何相关信息,而且已经过去了5年了!
总而言之,我的类或 MediaPlayer 控件可能遇到了一个大型事件未触发的问题。我需要一个救星来帮助我赶上 mediaOpenned 事件..
再次感谢您。
I'm trying to build a class where my thumbnail class is capturing a frame as a thumbnail of the videos users uploaded, things have gone good so far. I've written down the application on wpf app first and then decided to move to a class on my website as thumbnail.cs. Code is as shown below.
My problem is, the MediaOpenned event doesn't fire up so the execution cannot continue as its supposed to, i tried calling it myself and put a thread.sleep to handle the delay with the buffering of video and the code worked as i wanted. But i don't want to rely on sleep since it can be unstable due to server and heavy.. I need it to work with the event or something similar like that.
Any ideas or suggestions why event is not firing on .net? while working perfect on wpf? and what to do.
And millions of thanks of course!
thumbnail.cs class;
MediaPlayer myPlayer = new MediaPlayer();
public thumbnail()
{
myPlayer.Open(new Uri(@"C:\movie.wmv"));
myPlayer.Play();
myPlayer.IsMuted = true;
//this doesn't fire up
myPlayer.MediaOpened += new EventHandler(myPlayer_MediaOpened);
/*
when i do like this it works as i want but unstable and heavy because sleep
myPlayer_MediaOpened(null, null);
System.Threading.Thread.Sleep(5000);
*/
}
void myPlayer_MediaOpened(object sender, EventArgs e)
{
myPlayer.Position = TimeSpan.FromSeconds(myPlayer.NaturalDuration.TimeSpan.Seconds / 2);
RenderTargetBitmap RenderTarBitmap = new RenderTargetBitmap(400, 300, 1 / 96, 1 / 96, PixelFormats.Pbgra32);
DrawingVisual DVisual = new DrawingVisual();
DrawingContext DContext = DVisual.RenderOpen();
DContext.DrawVideo(myPlayer, new Rect(0, 0, 400, 300));
DContext.Close();
RenderTarBitmap.Render(DVisual);
myPlayer.Stop();
BitmapFrame BMPFrame = BitmapFrame.Create(RenderTarBitmap);
BitmapEncoder BMPEncoder = new JpegBitmapEncoder();
System.IO.FileStream FStream = new System.IO.FileStream(@"C:\movie.jpg", System.IO.FileMode.Create);
BMPEncoder.Frames.Add(BMPFrame);
BMPEncoder.Save(FStream);
FStream.Flush();
FStream.Close();
}
--------------------------------------------
UPDATE:
Allright so i've been trying to solve this for days now and god knows the person going to help me is directly going to heaven! Anyway i found that i'm not able to fire any event handlers related to MediaPlayer or my thumbnail.cs (which is hosted in app_code and called by a asmx webservice).
Tried some workarounds by using threads and implementing my own made event handlers checking whether movie is loaded or not.. no luck there either MediaPlayer doesn't let me touch its insides in another thread... And i didn't want to do more hack approach while able to solve with an easy event handler!
Searched the deepest google, found a thread on 2006 that a person assumed it may be a problem with x64 (well i'm using x64) but i couldn't found anything about that on msdn and hell its been 5 years!
So to sum up, i'm probably having a massive event not firing problem with either my class or my MediaPlayer control. And i need a saviour to help me catch the mediaOpenned event..
Thank you again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论