Reading Attribute Values from a CD or DVD
Hi,
I got the code below from http://msdn.microsoft.com/en-us/library/dd564233(VS.85).aspx when I run it in Visual Studio 2005 or 2008 I get this exception: System.Windows.Forms.AxHost.InvalidActiveXStateException was unhandled. Does anyone know how I can fix this error?
using WMPLib;
AxWMPLib.AxWindowsMediaPlayer Player;private void logDiscAttributes()
{
int iCDCount = 0; // Count of CD and DVD drives
int iAttrCount = 0; // Attribute count.
int iPLCount = 0; // Playlist item count.
IWMPCdromCollection cdCollection;
IWMPPlaylist playlist;
IWMPMedia media;
string strAttribName = "";
string strAttribValue = "";
string strText = "";
// Create a StreamWriter object to write
// the output to a file.
StreamWriter sw = new StreamWriter(@"c:\strOutFile.txt", true);
cdCollection = Player.cdromCollection;
iCDCount = cdCollection.count;
// Loop through the CD and DVD drives.
for (int i = 0; i < iCDCount; i++)
{
playlist = cdCollection.Item(i).Playlist;
// Loop through the playlist attributes.
iAttrCount = playlist.attributeCount;
for (int j = 0; j < iAttrCount; j++)
{
strText += "Playlist Attribute: \t";
strAttribName = playlist.get_attributeName(j);
strText += "\t" + strAttribName + "\t";
strAttribValue = playlist.getItemInfo(strAttribName);
strText += strAttribValue + "\n";
}
// Loop through the playlist.
iPLCount = playlist.count;
for (int k = 0; k < iPLCount; k++)
{
media = playlist.get_Item(k);
// Loop through the media attributes.
iAttrCount = media.attributeCount;
for (int m = 0; m < iAttrCount; m++)
{
strText += "Track or Chapter [" + m.ToString() + "]";
strAttribName = media.getAttributeName(m);
strText += "\t" + strAttribName + "\t";
strText += "Read Only: " + media.isReadOnlyItem(strAttribName) + "\t";
strAttribValue = media.getItemInfo(strAttribName);
strText += strAttribValue + "\n";
}
}
}
sw.Write(strText);
sw.Close();
MessageBox.Show(@"c:\strOutFile.txt" + " created");
}
- 편집됨once3007 2009년 11월 5일 목요일 오전 10:36
답변
- Hi,
The exception occurred in line:
cdCollection = Player.cdromCollection;
and the exception says the Player object has invalid state, so, beside allocate memory for Player object:
AxWMPLib.AxWindowsMediaPlayer Player = new AxWMPLib.AxWindowsMediaPlayer();
we also need to initialize its status, something like this:
Player.Enabled = true;
Player.Location = new System.Drawing.Point(100, 100);
Player.Name = "Player";
Player.Size = new System.Drawing.Size(400, 300);
Player.TabIndex = 0;
Player.URL =@"E:\test.wmv";
Player.settings.volume = 10;
// You have to create a method mediaPlayer_PlayStateChange() to handle this event.
Player.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(mediaPlayer_PlayStateChange);
// You have to create a method mediaPlayer_ErrorEvent() to handle this event.
Player.ErrorEvent += new EventHandler(mediaPlayer_ErrorEvent);
Anyway, why don't use Windows Media Player Control? you can add WMP ActiveX control to a .NET application through the toolbox in VS environment, you can start from here :)
Thanks,
Eric
Please remember to mark helpful replies as answers and unmark them if they provide no help.- 답변으로 표시됨eryangMSFT, Moderator2009년 11월 10일 화요일 오전 9:18
- Thanks Eric,
I will follow you suggestion.- 답변으로 표시됨once3007 2009년 11월 10일 화요일 오후 9:07
모든 응답
- Hi,
The method logDiscAttributes()is incomplete, do you mind posting a full version of that method?
What does the exception detail say? it will be appreciated if you can provide the call stack.
Thanks,
Eric
Please remember to mark helpful replies as answers and unmark them if they provide no help. Thanks Eric,
Full version of the method below. If I run it just as it is on http://msdn.microsoft.com/en-us/library/dd564233(VS.85).aspx I get this Error: Use of unassigned local variable 'Player'
When I change player to this: AxWMPLib.AxWindowsMediaPlayer Player = new AxWMPLib.AxWindowsMediaPlayer(); I get the call stack at the buttom of this page.
-------------------------------------------------------------------------------------------------------------------------------
using WMPLib;
AxWMPLib.AxWindowsMediaPlayer Player;
private void logDiscAttributes()AxWMPLib.AxWindowsMediaPlayer Player;
int iCDCount = 0; // Count of CD and DVD drives
int iAttrCount = 0; // Attribute count.
int iPLCount = 0; // Playlist item count.
IWMPCdromCollection cdCollection = null;
IWMPPlaylist playlist;
IWMPMedia media;
string strAttribName = "";
string strAttribValue = "";
string strText = "";
// Create a StreamWriter object to write
// the output to a file.
StreamWriter sw = new StreamWriter(@"c:\strOutFile.txt", true);
cdCollection = Player.cdromCollection;
iCDCount = cdCollection.count;
// Loop through the CD and DVD drives.
for (int i = 0; i < iCDCount; i++)
{
playlist = cdCollection.Item(i).Playlist;
// Loop through the playlist attributes.
iAttrCount = playlist.attributeCount;
for (int j = 0; j < iAttrCount; j++)
{
strText += "Playlist Attribute: \t";
strAttribName = playlist.get_attributeName(j);
strText += "\t" + strAttribName + "\t";
strAttribValue = playlist.getItemInfo(strAttribName);
strText += strAttribValue + "\n";
}
// Loop through the playlist.
iPLCount = playlist.count;
for (int k = 0; k < iPLCount; k++)
{
media = playlist.get_Item(k);
// Loop through the media attributes.
iAttrCount = media.attributeCount;
for (int m = 0; m < iAttrCount; m++)
{
strText += "Track or Chapter [" + m.ToString() + "]";
strAttribName = media.getAttributeName(m);
strText += "\t" + strAttribName + "\t";
strText += "Read Only: " + media.isReadOnlyItem(strAttribName) + "\t";
strAttribValue = media.getItemInfo(strAttribName);
strText += strAttribValue + "\n";
}
}
}
sw.Write(strText);
sw.Close();
MessageBox.Show(@"c:\strOutFile.txt" + " created");
}
----------------------------------------------------------------------------------------
call stack:
System.Windows.Forms.AxHost.InvalidActiveXStateException was unhandled
Message="Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown."
Source="AxInterop.WMPLib"
StackTrace:
at AxWMPLib.AxWindowsMediaPlayer.get_cdromCollection()
at AttributeValuesfromaCDorDVD.Form1.logDiscAttributes() in C:\Documents and Settings\ASP\My Documents\Visual Studio 2005\Projects\AttributeValuesfromaCDorDVD\AttributeValuesfromaCDorDVD\Form1.cs:line 35
at AttributeValuesfromaCDorDVD.Form1.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\ASP\My Documents\Visual Studio 2005\Projects\AttributeValuesfromaCDorDVD\AttributeValuesfromaCDorDVD\Form1.cs:line 90
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at AttributeValuesfromaCDorDVD.Program.Main() in C:\Documents and Settings\ASP\My Documents\Visual Studio 2005\Projects\AttributeValuesfromaCDorDVD\AttributeValuesfromaCDorDVD\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()- Hi,
The exception occurred in line:
cdCollection = Player.cdromCollection;
and the exception says the Player object has invalid state, so, beside allocate memory for Player object:
AxWMPLib.AxWindowsMediaPlayer Player = new AxWMPLib.AxWindowsMediaPlayer();
we also need to initialize its status, something like this:
Player.Enabled = true;
Player.Location = new System.Drawing.Point(100, 100);
Player.Name = "Player";
Player.Size = new System.Drawing.Size(400, 300);
Player.TabIndex = 0;
Player.URL =@"E:\test.wmv";
Player.settings.volume = 10;
// You have to create a method mediaPlayer_PlayStateChange() to handle this event.
Player.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(mediaPlayer_PlayStateChange);
// You have to create a method mediaPlayer_ErrorEvent() to handle this event.
Player.ErrorEvent += new EventHandler(mediaPlayer_ErrorEvent);
Anyway, why don't use Windows Media Player Control? you can add WMP ActiveX control to a .NET application through the toolbox in VS environment, you can start from here :)
Thanks,
Eric
Please remember to mark helpful replies as answers and unmark them if they provide no help.- 답변으로 표시됨eryangMSFT, Moderator2009년 11월 10일 화요일 오전 9:18
- Thanks Eric,
I will follow you suggestion.- 답변으로 표시됨once3007 2009년 11월 10일 화요일 오후 9:07

