I've been recently getting around a lot of issues with the
DirectX.AudioVideoPlayback.Audio class.
It's a difficult class to work with, bad enough to make someone write this
article:
www.codeproject.com/cs/media/DirectX9_media_playback.asp
I however don't want to use Media Player as the author of this article
recommends, so i'm plugging away at sorting out the last of the remaining
problems.
One of the last issues remaining is an odd one, very occasionally I get an
"Error in Application" exception when opening a file.
After checking out the DirectX exception I got the error string:
CO_E_NOTINITIALIZED
After checking this on Google it seems that a call to CoInitializeEx is
required to resolve this issue and i'm assuming I have this issue as I open
the object on a new thread (yet another work around to make the events work
properly). I've done a simple P/Invoke before so I have the necessary code
for the method import, the only thing I don't know is the value of
COINIT_APARTMENTTHREADED or COINIT_MULTITHREADED.
How do I find out what these values are?
So I can do this:
const uint COINIT_APARTMENTTHREADED = ??;
const uint COINIT_MULTITHREADED = ??;
CoInitialize(null, COINIT_APARTMENTTHREADED);
-
OR
-
CoInitialize(null, COINIT_MULTITHREADED);
Also, is this a good idea and might it resolve my problem or open up a can
of worms? (as I have limited data about what this method does exactly)
Your thoughts please.
The code that i'm having problems with is:
private void InitAudio() /// this gets called the FIRST time a file is
played only
{
ThreadStart ts = new ThreadStart(AudioThreadStart);
audioThread = new Thread(ts);
audioThread.Name = "CynoPlayerAudio.InitAudio() Thread";
audioThread.Priority = ThreadPriority.Highest;
audioThread.Start();
}
private void AudioThreadStart()
{
try
{
audio = new Audio(threadArgument, false); /// this is where I sometimes
get "Error in Application" = CO_E_NOTINITIALIZED
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show(ex.Message);
}
audio.Ending+=new EventHandler(audio_Ending);
audio.Pausing+=new EventHandler(audio_Pausing);
audio.Starting+=new EventHandler(audio_Starting);
audio.Stopping+=new EventHandler(audio_Stopping);
if(autoRun)
{
audio.Play();
}
Application.Run(); /// this line is required to make the events for the
audio object to work as it relies (undocumented of course) on the windows
message system
}
Please help if you can. :)
Kind Regards
Jax
Robert Jordan - 16 Sep 2005 14:07 GMT
Hi,
> private void InitAudio() /// this gets called the FIRST time a file is
> played only
[quoted text clipped - 3 lines]
> audioThread.Name = "CynoPlayerAudio.InitAudio() Thread";
> audioThread.Priority = ThreadPriority.Highest;
You forgot to initialize the STA:
audioThread.ApartmentState = ApartmentState.STA;
> audioThread.Start();
> }
Rob
Uchiha Jax - 16 Sep 2005 15:00 GMT
Problem solved. Many thanks.
Jax
> Hi,
>
[quoted text clipped - 14 lines]
>
> Rob