I'm trying to get the current time of a song when is playing, but when a call to MediaPlayer.PlayPosition.TotalMilliseconds;
is made, an error is thrown:
An unhandled exception of type 'SharpDX.SharpDXException' occurred in SharpDX.dll
Additional information:
HRESULT: [0xC00D9C41],
Module: [Unknown],
ApiCode: [Unknown/Unknown],
Message: Unknown
I'm learning c#
and monogame
and maybe I'm commiting an obvious mistake.
The code that throw that error is this:
public static void SpawnArcker(GameTime gametime)
{
if (MediaPlayer.State != MediaState.Playing) return;
elapsedtime += (float) MediaPlayer.PlayPosition.TotalMilliseconds; // <--- HERE
//elapsedtime += (float) gametime.ElapsedGameTime.TotalMilliseconds;
...
}
This is being called in the update method of the principal loop like this:
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if (Keyboard.GetState().IsKeyDown(Keys.Space))
MediaPlayer.Play(Media.song);
SpawnArcker(gameTime);
...
}
I though it was because I was playing large .wav files, but the same happen with short .mp3 files.
So if someone is interested...
After a while, I found (I guess) the meaning of this error. In this page it says:
0xC00D9C41
#define MF_E_CLOCK_NO_TIME_SOURCE
No Presentation Time Source has been specified.
Then, I search for that error and found this where it says:
The presentation clock does not return a valid time until the Media Session sends the MESessionTopologyStatus event with the MF_TOPOSTATUS_READY flag. Until then, GetClock returns MF_E_CLOCK_NO_TIME_SOURCE.
(Emphasys added)
I guess is the fact that, even when the song is "Playing", there is no time yet to get, so this error is thrown.
Too much for me, so I opt to try a different library called NAudio with a similar functionality, so I reemplace everything related to MediaPlayer
with this library and, with the same logic, now it works. This is some of the changes I made:
public static void SpawnArcker(WaveOutEvent waveOutDevice, Mp3FileReader audioFileReader)
{
if (waveOutDevice.PlaybackState != PlaybackState.Playing) return;
elapsedtime += (float) audioFileReader.CurrentTime.TotalMilliseconds - aux_elapsedtime;
aux_elapsedtime = (float) audioFileReader.CurrentTime.TotalMilliseconds;
//... rest of code...
}
User contributions licensed under CC BY-SA 3.0