Playing an .avi file in a C# app.

A

Allan Michaels

Prequel to the question:

We sell instruments to industry.

These instruments are controlled by a Windows system.

These windows systems are Windows XP x64 systems, and my software MUST run
in 64-bit mode (not win32 on a 64-bit system.)

These systems are 'locked down' so the user on the factory floor cannot
access the OS. No IE. No Media Players. Nothing. Every feature and
function must be available through our software.


The question:

We need to show instructional videos in our software. We are thinking AVI
is what we want. (But flash would be OK since our videos are originally
created as flash.)

As described in the prequel above, the video player must somehow be embedded
into our software. Mostly because we want the user to ONLY be able to play
our videos.

How should I proceed?

Please remember that this needs to work on a Windows XP x64 running native
x64 software (not win32 running on x64).
I believe that precludes the use of managed DirectX since managed DirectX is
not supported under native x64.

Lastly, I have implemented a native x64 DirectX 3D Viewer in our software
using C++ .dlls called from our C# code.

Thank you in advance for your help.
 
J

Joe Cool

Prequel to the question:

We sell instruments to industry.

These instruments are controlled by a Windows system.

These windows systems are Windows XP x64 systems, and my software MUST run
in 64-bit mode (not win32 on a 64-bit system.)

These systems are 'locked down' so the user on the factory floor cannot
access the OS.  No IE.  No Media Players.  Nothing.  Every feature and
function must be available through our software.

The question:

We need to show instructional videos in our software.  We are thinking AVI
is what we want.  (But flash would be OK since our videos are originally
created as flash.)

As described in the prequel above, the video player must somehow be embedded
into our software.  Mostly because we want the user to ONLY be able to play
our videos.

How should I proceed?

Please remember that this needs to work on a Windows XP x64 running native
x64 software (not win32 running on x64).
I believe that precludes the use of managed DirectX since managed DirectXis
not supported under native x64.

Lastly, I have implemented a native x64 DirectX 3D Viewer in our software
using C++ .dlls called from our C# code.

Thank you in advance for your help.

I would use the MediaPlayer control.

I have used it to play MP3 files, I'm sure it can be used to play VI
files as well.
 
A

Allan Michaels

I am working with Visual Studio 2005 on Windows XP Professional x64 Edition.

I created a brand new C# WindowsApplication1.sln with a Form1.cs.
The solution is configured to build for Any CPU because this is required for
us.

I added the Windows Media Player control to Form1. (After I added the
control to my toolbox.)

I ran the program and got the "WindowsApplication1 has encountered a problem
and needs to close. We are sorry for the inconvenience".

I deleted the control from Form1, and the program successfully showed Form1
(without the Media Player of course.)

====

Then I did the same exact thing on a VisStudio2005 on WindowsXP Professional
(32-bit version).
Form1 appeared with the media controler.

=====

Then I took the app built on the 32-bit system for Any CPU,
and I copied to the 64-bit system. I ran it, and it failed like in the
first example above.

=============

Joe, looks like this doesn't work for x64.

So I still need to find a way to play an AVI in a C# application that is
built for ANY CPU and that will run on a 64-bit windows system.
 
J

Jie Wang [MSFT]

Hello Allan,

The Windows Media Player control does have a x64 version on Windows XP x65
Edition. I'm not sure why it crashes, but using a debugger can help finding
out the root cause.

If you don't want to do that, you may try upgrade the Media Player to
version 11 and see if the problem sovles:
http://www.microsoft.com/windows/windowsmedia/download/AllDownloads.aspx

From the link above, choose WMP 11 for Windows XP x64, download and setup.
By the way, is the crash happens on all of your x64 XP machines?

Also, there is another way to play the AVI inside your application without
using the WMP control.

You can use the Windows MCI functions to control the video playback in your
WinForm application.

I wrote some sample code on how to do that, please create a new WinForm
project to test it out:

1. Put a picture box named picPlayer on the form. Set the picPlayer's size
to your video's size, or at least keep the same width:hight ratio.
2. Put a text box named txtFileName on the form. Set the text to the full
path of your AVI file.
3. Put three buttons named btnPlay, btnPause, btnStop on the form.
4. Paste the following code into the form's code, and import the
System.Runtime.InteropServices namespace.

[DllImport("winmm.dll", CharSet = CharSet.Auto)]
private static extern Int32 mciSendString(
String command,
StringBuilder buffer,
Int32 bufferSize,
IntPtr hwndCallback);

[DllImport("winmm.dll", CharSet = CharSet.Auto)]
private static extern Int32 mciGetErrorString(Int32 errorCode,
StringBuilder errorText, Int32 errorTextSize);

private const string VideoAlias = "myVideo";

private void btnPlay_Click(object sender, EventArgs e)
{
string cmd;

// open the device
cmd = string.Format("open \"{0}\" alias {1} wait",
txtFileName.Text.Trim(), VideoAlias);
SendMCIString(cmd, IntPtr.Zero);

// set the picture box as player window
cmd = string.Format("window {0} handle {1} wait", VideoAlias,
picPlayer.Handle);
SendMCIString(cmd, IntPtr.Zero);

// set the destination rect
cmd = string.Format("put {0} destination at {1} {2} {3} {4} wait",
new object[] { VideoAlias, 0, 0, picPlayer.Width, picPlayer.Height
});
SendMCIString(cmd, IntPtr.Zero);

// start playing
cmd = string.Format("play {0}", VideoAlias);
SendMCIString(cmd, IntPtr.Zero);
}

private void btnPause_Click(object sender, EventArgs e)
{
string cmd = string.Format("status {0} mode", VideoAlias);
// first check the playing status
if (SendMCIString(cmd, IntPtr.Zero).ToLowerInvariant() == "paused")
{
// if already paused, then play
cmd = string.Format("play {0}", VideoAlias);
}
else
{
// if already playing, then pause
cmd = string.Format("pause {0}", VideoAlias);
}
SendMCIString(cmd, IntPtr.Zero);
}

private void btnStop_Click(object sender, EventArgs e)
{
// close the device
string cmd = string.Format("close {0}", VideoAlias);
SendMCIString(cmd, IntPtr.Zero);
}

private string SendMCIString(string mciString, IntPtr hWndCallback)
{
StringBuilder buffer = new StringBuilder(512);
int errCode;

errCode = mciSendString(mciString, buffer, buffer.Capacity,
hWndCallback);

// If error occurred, get the error text and throw exception
if (errCode != 0)
{
buffer = new StringBuilder(256);
mciGetErrorString(errCode, buffer, buffer.Capacity);
throw new ApplicationException(buffer.ToString());
}

return buffer.ToString();
}

5. Hook up the buttons' events with the code, like btnPlay_Click etc.
6. Run the project and see if that works for you.

I've tested the above code on my x64 system (but not XP), please let me
know if that works for you.

Best regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Allan Michaels

Hello Jie Wang,

Thanks for the two ideas (including all that code).

It will take a day or so to work though the two ideas.
I will reply soon after.

Thanks Jie Wang.
 
J

Jie Wang [MSFT]

No problem. I'll keep watching this post.

Let me know if any help needed.

Regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Allan Michaels

Hello Jie Wang,

I have tried the first half of your suggestions. I have not used your code
yet.

I updated to WMP 11 for XP x64. The test app crashed while running on WinXP
x64.
Using the debugger, I retrieved the info:

COMException was unhandled
Class not registered (Exception from HRESULT: 0x80040154

(REGDB_E_CLASSNOTREG)

View Detail link displays a View Detail window:

System.Runtime.InteropServices.COMException

The StackTrace

at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid&

clsid, Object punkOuter, Int32 context, Guid& iid)
at System.Windows.Forms.AxHost.CreateWithoutLicense(Guid clsid)
at System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid

clsid)
at System.Windows.Forms.AxHost.CreateInstanceCore(Guid clsid)
at System.Windows.Forms.AxHost.CreateInstance()
at System.Windows.Forms.AxHost.GetOcxCreate()
at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)
at System.Windows.Forms.AxHost.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.AxHost.EndInit()
at WindowsApplication1.Form1.InitializeComponent() in

C:\Products\Software\trunk\ClassLibraries\MediaPlayerTest\WindowsAp

plication1\Form1.Designer.cs:line 57
at WindowsApplication1.Form1..ctor() in

C:\Products\Software\trunk\ClassLibraries\MediaPlayerTest\WindowsAp

plication1\Form1.cs:line 13
at WindowsApplication1.Program.Main() in

C:\Products\Software\trunk\ClassLibraries\MediaPlayerTest\WindowsAp

plication1\Program.cs:line 14
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext

executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

=========================================

I changed the test app configuration from building "Any CPU" to building
"x64". The test app crashed again running on WinXP x64.

I changed the test app configuration to building "win32". The test app did
NOT crash running in WinXP x64.

Note: when I added the Windows Media Player Control to the toolbox via the
Choose Toolbox items window...the control indicated it was located at
C:\Windows\syswow64\wmp.dll.

Currently, our application must be built as Any CPU to run on win32
platforms and to run in full 64-bit mode on x64 platforms.


Next, I will try your code.

And, again, thank you for your help.
 
J

Jie Wang [MSFT]

Hi Allan,

I also did some researches on WMP11. It seems the WMP 11 doesn't provide an
x64 version on Windows XP x64 (while it does on Windows Vista x64). That's
why your application is getting the Class not registered exception.

So... yes, we need to give a try on the MCI functions.

Regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top