|
Great Article.
Constructor of the Texture2D has changed in the new version.
So if you want to use it with the new version, first
update the xna references so that you are referencing
to 2.0 and then change the lines where the texture is created
this.videoTexture2D = new Texture2D(this.graphics, this.width, this.height, 1, TextureUsage.None, SurfaceFormat.Color);
Cheers,
Mert
|
|
|
|
|
Thanks for noting this Mert.
There are a couple of other changes that are needed in the code to make it compile and run without error, and these can be found in the message "Changes Necessary for XNA 2.0" below.
I guess I really should upload the modified code so that people don't have this difficulty...
Cheers!
Steve.
|
|
|
|
|
I made a small test and works very well, but I have just a small question, where is the sound?
By the code it just read the frame and convert to a texture, now how can I play a video intro for example with sound support?
Thanks for the help and very nice component
|
|
|
|
|
Hello,
i have the same question. I have tryed to add a sound device to the graph, but ... i dont realy know what i have to do.
Have anybody some inpirations?
Best Regards,
Thorsten
|
|
|
|
|
I used your code successfully to play video from a tv card but I need to play from the composite video input of the tv card.
Can you give me some clues
|
|
|
|
|
XNA 2.0 made some "breaking changes" from XNA 1.0 that impact this code. Thanks to Odinlai for bringing this issue to my attention. To use this code with XNA Framework 2.0 change references to Microsoft.Xna.Framework and Microsoft.Xna.Framework.Game from version 1.0 to 2.0 and make the following changes in the code files:
In VideoComponent.cs
FROM (248): private void LoadContent()
TO (248): protected override void LoadContent()
In VideoTexture.cs
FROM (155): this.videoTexture2D = new Texture2D(this.graphics, this.width, this.height, 1, ResourceUsage.None, SurfaceFormat.Color, ResourceManagementMode.Automatic);
TO (155): this.videoTexture2D = new Texture2D(this.graphics, this.width, this.height, 1, TextureUsage.None, SurfaceFormat.Color);
FROM (190): this.videoTexture2D = new Texture2D(this.graphics, this.width, this.height, 1, ResourceUsage.None, SurfaceFormat.Color, ResourceManagementMode.Automatic);
TO (190): this.videoTexture2D = new Texture2D(this.graphics, this.width, this.height, 1, TextureUsage.None, SurfaceFormat.Color);
INSERT BETWEEN (618-619): this.graphics.Textures[0] = null; // Workaround suggested in https://connect.microsoft.com/feedback/Workaround.aspx?FeedbackID=318195&SiteID=226
In xnaVideoViewer.cs
INSERT BETWEEN (96-97): [Obsolete]
INSERT BETWEEN (115-116): [Obsolete]
The next update to this site will include these changes. In the meantime, please make the changes manually if you are affected by this issue.
Thanks.
Steve.
|
|
|
|
|
when i compile the VideoTexture.cs
an error occur:
Error 1 The name 'ResourceUsage' does not exist in the current context D:\fun project\webcam with xna\webcam with xna\VideoTexture.cs 155 92 webcam with xna
is my setting is wrong??
please help
|
|
|
|
|
Thanks for bringing this error to my attention.
Unfortunately, ResourceUsage has been removed from XNA Framework 2.0, creating a breaking change from XNA Framework 1.0. Methods that used to take ResourceUsage now take BufferUsage or TextureUsage. The Texture2D constructor in particular now requires a TextureUsage. This is also a breaking change from XNA Framework 1.0.
The easiest way to make the software work is to reference the Microsoft.Xna.Framework and Microsoft.Xna.Framework.Game libraries for Version 1.0. I realize that this is unsatisfactory for anyone using the 2.0 frameworks (which I would prefer to use myself...)
I have made the changes necessary to compile the program under XNA 2.0. This required changing the following lines:
FROM:
this.videoTexture2D = new Texture2D(this.graphics, this.width, this.height, 1, ResourceUsage.None, SurfaceFormat.Color, ResourceManagementMode.Automatic);//Version 1.0
TO:
this.videoTexture2D = new Texture2D(this.graphics, this.width, this.height, 1, TextureUsage.None, SurfaceFormat.Color);// Version 2.0
A few warnings are now generated about obsolete methods, and these should also be corrected.
Although this compiles without error, the following line now generates a runtime framework error:
this.videoTexture2D.SetData<Microsoft.Xna.Framework.Graphics.Color>(colorData);
The error is:
"A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.dll"
This error can be thrown if the number of elements in the array colordata is not the same as the size of the Texture2D (it is), or if the Texture2D resource is currently set on the graphics device (maybe it is.) This looks like it is also an (undocumented) breaking change from XNA 1.0. I am afraid that more work will be needed to resolve this problem...
Steve.
|
|
|
|
|
I have been able to confirm this bug in the XNA 2.0 framework. Fortunately there is a workaround:
Change the VideoTexture.BufferCB() method to the following:
/// <summary>
/// Buffer Callback method from the DirectShow.NET ISampleGrabberCB interface. This method is called
/// when a new frame is grabbed by the SampleGrabber.
/// </summary>
/// <param name="SampleTime">The sample time.</param>
/// <param name="pBuffer">A pointer to the image buffer that contains the grabbed sample.</param>
/// <param name="BufferLen">The length of the image buffer containing the grabbed sample.</param>
/// <returns>0 = success.</returns>
public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
byte[] bgrData = new byte[BufferLen];
Marshal.Copy(pBuffer, bgrData, 0, BufferLen);
Color[] colorData = new Color[BufferLen / 3];
for (int i = 0; i < colorData.Length; i++)
{
colorData[i] = new Microsoft.Xna.Framework.Graphics.Color(bgrData[3 * i + 2], bgrData[3 * i + 1], bgrData[3 * i]);
}
// The following line works in XNA 1.0 but generates an error in XNA 2.0
this.graphics.Textures[0] = null; // Workaround suggested in https://connect.microsoft.com/feedback/Workaround.aspx?FeedbackID=318195&SiteID=226
this.videoTexture2D.SetData<Microsoft.Xna.Framework.Graphics.Color>(colorData);
RaiseNewVideoTextureEvent(); // notify subscribers, if any.
return 0;
}
This should allow the code to operate correctly under XNA 2.0.
Steve.
|
|
|
|
|
I can't create two video components to play two different videos simultaneously.
|
|
|
|
|
Hi Lars;
For some unknown reason Codeproject is not allowing me to reply to your post today, hence this new post.
The size of the texture is set by inspecting the size of the source video. If you want to have a larger texture then you will need to change the output size from your webcam, if you webcam allows it.
Steve.
-- modified at 12:34 Thursday 11th October, 2007
|
|
|
|
|
This is really a nice little class.
Unfortuantely it only grabs pictures sized 320*240 on my Pc. Could you tell me how I could increase the resolution?
Greetings Lars
|
|
|
|
|
In order to integrate in my current project i have added Videotexture and videocomponent classes and referenced direcshowlib.
It compiles well.
I have added
TV = new VideoComponent(this);
this.Components.Add(TV);
in initialize method of game1.cs
and
TV.VideoTexture.Start(); (ive tried TV.VideoTexture.Run() also)
in the game1 update method
it compiles but is not showing anything.
(i am using a public static spritebatch declared in game1 in the video componet class not the videosprite spritebatch declared in videocomponent.
|
|
|
|
|
There are some arguments of wrong type
What version of directxlib should i use?
The binaries (xnaviewer.exe) does not work, it crashes too.The two dlls are in the same folder that the .exe.
I am trying to play an .avi there is no webcam installed. The final goal of the prject is play video from a TV card.
directxlib used:
v 2.0 - (2007-07-15)
Errors (in spanish sorry )
--------------------------------------------------------------
Error 4 La mejor coincidencia de método sobrecargado para 'DirectShowLib.IMediaEventEx.FreeEventParams(DirectShowLib.EventCode, System.IntPtr, System.IntPtr)' tiene algunos argumentos no válidos C:\Documents and Settings\XnaVideoProjectFiles\xnaVideoProjectFiles\xnaVideoLib\VideoTexture.cs 595 26 xnaVideoLibary
Error 2 Argumento '2': no se puede convertir de 'out int' a 'out System.IntPtr' C:\Documents and Settings\XnaVideoProjectFiles\xnaVideoProjectFiles\xnaVideoLib\VideoTexture.cs 570 70 xnaVideoLibary
Error 3 Argumento '3': no se puede convertir de 'out int' a 'out System.IntPtr' C:\Documents and Settings\XnaVideoProjectFiles\xnaVideoProjectFiles\xnaVideoLib\VideoTexture.cs 570 87 xnaVideoLibary
Error 4 La mejor coincidencia de método sobrecargado para 'DirectShowLib.IMediaEventEx.FreeEventParams(DirectShowLib.EventCode, System.IntPtr, System.IntPtr)' tiene algunos argumentos no válidos C:\Documents and Settings\XnaVideoProjectFiles\xnaVideoProjectFiles\xnaVideoLib\VideoTexture.cs 595 26 xnaVideoLibary
Error 5 Argumento '2': no se puede convertir de 'int' a 'System.IntPtr' C:\Documents and Settings\XnaVideoProjectFiles\xnaVideoProjectFiles\xnaVideoLib\VideoTexture.cs 595 71 xnaVideoLibary
Error 6 Argumento '3': no se puede convertir de 'int' a 'System.IntPtr' C:\Documents and Settings\XnaVideoProjectFiles\xnaVideoProjectFiles\xnaVideoLib\VideoTexture.cs 595 84 xnaVideoLibary
|
|
|
|
|
Answering myself :
//int eventParam1, eventParam2;
IntPtr eventParam1, eventParam2
------------------------------------------------------------
Now another question. I I connect a tv receiver card can i use yourcomponent to play video from it?
|
|
|
|
|
Answering myself again.
Yes it works
|
|
|
|
|
Hi Stephen
I posted many requests for several forums to get a component for streaming video in network.
I developed a software for a medical clinics. Doctors like to see Waiting Room.
My software installed in two computers. One in clerck's room and another in doctor's room and they work together. I connected webcam to clerck's computer and i want to send video to doctor's computer.
Is your code can help me about this. before i used MediaEncoder that it has delay and its not usefull.
Tanx
|
|
|
|
|
The VideoTexture class is not directly usable by a remote object because it is not a MarshalByRef object, and it depends upon a GraphicsDevice object that binds it to a specific context.
However, the source could be worked fairly easily into a remotable object that would, for example, pass width, height, and an array of colorData through an EventArg using a Client-Server model. The client could update a VideoTexture object using these arguments.
This sounds like a worthy extension, and I may do it if I have time. Unfortunately, I will be travelling for the next month and will not be able to consider it until I return in mid October.
Steve.
|
|
|
|
|
Hi mister,
any solution about it ? some code source please ?
Regards
AE
|
|
|
|
|
Esto no tira ni empujando
One word witch two different miniuns funy ingles
|
|
|
|
|
Me disculpo por mi español pobre. Si entiendo tu comentario, no puedes recibir el NewVideoTextureEvent cuando suscribes. ¿Es este correcto? ¿Se levanta una excepción?
|
|
|
|
|
Is it possible to play a movie file with voice synchronously in XNA?
|
|
|
|
|
I am sure that it can be done; however this project has not attempted to work with the audio tracks of the video files, which are simply ignored by the DirectShow filter graphs that I am using. If you use XACT to pull your sound files into the XNA project, then you would have to try to synchronize the audio and video yourself, I am afraid.
Steve.
|
|
|
|
|
Yes XACT could be used as an alternative but it would be great to use direct show to play the movie with audio anyway...
|
|
|
|
|
Hi,
I can't set the destination rectangle for Texture2D. Actualy the scene does not fit in the textures scale and also by default VideoTexture2D are rotated by 90 degrees. I have tested the component with a mpeg file.
Thanx for the great job though
|
|
|
|