This code used to work before I updated MonoGame (develop branch).
On my menu screen I create a VideoPlayer and start a Video that I loop manually. When navigating to another menu screen, the old VideoPlayer is disposed and a new menu screen object gets created that has a new VideoPlayer. But now the video can not be played - it times out and gives the exception “cannot start video”.
This is the code that throws the exception:
/// <summary>
/// Plays a Video.
/// </summary>
/// <param name="video">Video to play.</param>
public void Play(Video video)
{
if (video == null)
throw new ArgumentNullException("video is null.");
if (_currentVideo == video)
{
var state = State;
// No work to do if we're already
// playing this video.
if (state == MediaState.Playing)
return;
// If we try to Play the same video
// from a paused state, just resume it instead.
if (state == MediaState.Paused)
{
PlatformResume();
return;
}
}
_currentVideo = video;
PlatformPlay();
_state = MediaState.Playing;
// XNA doesn't return until the video is playing
const int retries = 5;
const int sleepTimeFactor = 50;
for (int i = 0; i < retries; i++)
{
if (State == MediaState.Playing )
{
break;
}
var sleepTime = i*sleepTimeFactor;
Debug.WriteLine("State != MediaState.Playing ({0}) sleeping for {1} ms", i + 1, sleepTime);
#if WINRT
Task.Delay(sleepTime).Wait();
#else
Thread.Sleep(sleepTime); //Sleep for longer and longer times
#endif
}
if (State != MediaState.Playing )
{
//We timed out - attempt to stop to fix any bad state
Stop();
throw new InvalidOperationException("cannot start video");
}
}