SubtitleEdit/subtitleedit · error · InvalidOperationException

MpvPlayer is not initialized

Error message

MpvPlayer is not initialized

What it means

InvalidOperationException from OnInitialized: the control's _mpvPlayer dependency is null. The Avalonia control was instantiated (e.g. by the XAML designer/previewer, or without dependency injection) before a player instance was supplied.

Source

Thrown at src/ui/Logic/VideoPlayers/LibMpvDynamic/LibMpvDynamicSoftwareControl.cs:35

    private WriteableBitmap? _renderTarget;
    private bool _isInitialized;

    public LibMpvDynamicPlayer? Player => _mpvPlayer;

    public LibMpvDynamicSoftwareControl(LibMpvDynamicPlayer mpvPlayer)
    {
        _mpvPlayer = mpvPlayer;
        ClipToBounds = true;
        Cursor = new Cursor(StandardCursorType.Arrow);
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();

        if (_mpvPlayer == null)
        {
            throw new InvalidOperationException("MpvPlayer is not initialized");
        }

        System.Diagnostics.Debug.WriteLine("Initializing MpvPlayer with software rendering");

        try
        {
            _mpvPlayer.InitializeWithSoftwareRendering();
            _mpvPlayer.PlayerSubName = "sw";
            _mpvPlayer.RequestRender += OnMpvRequestRender;
            _isInitialized = true;
            System.Diagnostics.Debug.WriteLine("MpvPlayer initialized successfully with software rendering!");
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine($"Failed to initialize MpvPlayer: {ex.Message}");
        }
    }

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Ensure the mpvPlayer is provided to the control before it initializes (resolve via the app's service provider / DI).
  2. Guard OnInitialized with a design-mode check so the designer does not trigger initialization.
  3. Register the player service in the application's DI container.

Example fix

// before
protected override void OnInitialized()
{
    base.OnInitialized();
    if (_mpvPlayer == null) throw new InvalidOperationException("MpvPlayer is not initialized");
    _mpvPlayer.InitializeWithSoftwareRendering();
}

// after
protected override void OnInitialized()
{
    base.OnInitialized();
    if (Design.IsDesignMode || _mpvPlayer == null) return;
    _mpvPlayer.InitializeWithSoftwareRendering();
}
Defensive patterns

Strategy: validation

Validate before calling

if (Design.IsDesignMode || _mpvPlayer == null) return;

Prevention

When it happens

Trigger: The XAML designer or previewer instantiates the control with no DI; the control is constructed before its mpvPlayer is injected; or the player service was never registered in the DI container.

Common situations: Design-time preview, a wiring bug where the player was not passed to the control, or a refactor that dropped the DI registration.

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/9ab8781a111db5a0. Report an issue: GitHub.