Can no longer create a VideoPlayer and play a Video for a 2nd time

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"); 
            }
        }

Here is code that can repro this:

        VideoPlayer player = new VideoPlayer();
        player.Play(video); 

        player.Stop();
        player.Dispose();

// try a 2nd time:
        player = new VideoPlayer();
        player.Play(video); //<-- throws exception "cannot start video"

This is a Windows DX project, right?

The only change that was made there is https://github.com/MonoGame/MonoGame/commit/8821833913cbb7240890223646e820f00dcc8d00
I doesn’t look like those changes can cause this issue though. We also updated SharpDX if you got a very recent develop version (less than 4 days old), that seems like a more likely culprit to me.

I’ve got a pending PR that fixes many issues with the DirectX VideoPlayer. I’ll rebase it on latest develop branch and see what it does with this test case.

1 Like