dotnet/wpf · error · NotSupportedException

SR.AudioVideo_CannotControlMedia

Error message

SR.AudioVideo_CannotControlMedia

What it means

AVElementHelper.SetState throws this NotSupportedException when attempting to set the media state (Play/Pause/Stop etc.) while both the loaded and unloaded behaviors are non-Manual (e.g. Play/Close/Pause). With automatic behaviors, direct state control of the MediaElement is not permitted — only Manual behavior allows imperative control.

Solutions

  1. Set LoadedBehavior="Manual" (and/or UnloadedBehavior="Manual") on the MediaElement before calling Play/Pause/Stop.
  2. If you rely on automatic playback, remove the imperative Play call instead.
  3. Set behaviors in code: media.LoadedBehavior = MediaState.Manual;

Example fix

<!-- before -->
<MediaElement x:Name="media" LoadedBehavior="Play" Source="a.wmv"/>
<!-- after -->
<MediaElement x:Name="media" LoadedBehavior="Manual" Source="a.wmv"/>
<!-- then call media.Play() in code -->
Defensive patterns

Strategy: validation

Validate before calling

if (media.LoadedBehavior != MediaState.Manual && media.UnloadedBehavior != MediaState.Manual)
    throw new InvalidOperationException("Set LoadedBehavior or UnloadedBehavior to Manual before imperative control");

Type guard

bool CanControlManually(MediaElement m) => m.LoadedBehavior == MediaState.Manual || m.UnloadedBehavior == MediaState.Manual;

Try / catch

try { media.Play(); } catch (NotSupportedException) { media.LoadedBehavior = MediaState.Manual; media.Play(); }

Prevention

When it happens

Trigger: Calling MediaElement.Play/Pause/Stop (which route through SetState) when LoadedBehavior and UnloadedBehavior are both set to values other than Manual (the default is Play).

Common situations: XAML like `<MediaElement LoadedBehavior="Play" ... />` followed by code-behind media.Play(); default behavior is Play, so any imperative call before changing it fails.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/d223babd17ae64ec. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AVElementHelper.cs:271

            _speedRatio._wasSet = _speedRatio._isSet = true;
            _speedRatio._value = speedRatio;

            HandleStateChange();
        }

        internal
        void
        SetState(
            MediaState      mediaState
            )
        {
            //
            // If the caller hasn't requested any loaded or unloaded behavior to be manual
            // then calls to Play/Pause/Stop etc. will never take effect.
            //
            if (_loadedBehavior != MediaState.Manual && _unloadedBehavior != MediaState.Manual)
            {
                throw new NotSupportedException(SR.AudioVideo_CannotControlMedia);
            }

            _mediaState._value = mediaState;
            _mediaState._isSet = true;

            HandleStateChange();
        }

        internal
        void
        SetVolume(
            double          volume
            )
        {
            _volume._wasSet = _volume._isSet = true;
            _volume._value = volume;

            HandleStateChange();

View on GitHub (pinned to 81131a70a4)