{"record":{"id":"34201f2f568a6d93","repo":"SubtitleEdit/subtitleedit","slug":"failed-to-initialize-mpv-player-geterrorstring-e","errorCode":null,"errorMessage":"Failed to initialize mpv: {player.GetErrorString(err)}","messagePattern":"Failed to initialize mpv: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ui/Features/Video/TextToSpeech/TextToSpeechViewModel.cs","lineNumber":2227,"sourceCode":"            {\n                SeLogger.Error(ex, \"TTS window: disposing the audio preview player failed\");\n            }\n        });\n    }\n\n    private async Task PlayAudio(string fileName)\n    {\n        DisposePreviewPlayer();\n\n        LibMpvDynamicPlayer player;\n        lock (_playLock)\n        {\n            player = new LibMpvDynamicPlayer();\n            player.LoadLib(); // core not initialized\"\n            var err = player.Initialize();\n            if (err < 0)\n            {\n                throw new InvalidOperationException($\"Failed to initialize mpv: {player.GetErrorString(err)}\");\n            }\n\n            _mpvContext = player;\n        }\n\n        // Through the local: a close running now can null the field out from under us.\n        await player.LoadAudio(fileName);\n    }\n\n    private async Task<bool> IsEngineInstalled(ITtsEngine engine)\n    {\n        return await TtsEngineInstaller.EnsureEngineInstalled(engine, Window, _windowService, SelectedRegion, SelectedModel, ApiKey, KeyFile, () => RefreshVoices(engine));\n    }\n\n    private async Task MergeAndAddToVideo(TtsStepResult[] fixSpeedResult)\n    {\n        // Merge audio paragraphs\n        var mergedAudioFileName = await MergeAudioParagraphs(fixSpeedResult, _cancellationToken);","sourceCodeStart":2209,"sourceCodeEnd":2245,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Features/Video/TextToSpeech/TextToSpeechViewModel.cs#L2209-L2245","documentation":"Thrown inside TextToSpeechViewModel.PlayAudio when a freshly constructed LibMpvDynamicPlayer fails Initialize() during an audio preview in the main Text-to-Speech window. The code disposes any previous preview player, then under _playLock builds a new player, LoadLib(), and Initialize(); a negative return is converted to this InvalidOperationException. As written (lines 2215-2235) the throw is not caught locally — it propagates to the command/preview caller. A subsequent close can null _mpvContext out from under the awaited LoadAudio, which is the separate race noted in the comment.","triggerScenarios":"libmpv present but mpv_initialize returns negative: wrong arch/bitness of libmpv, missing VC++ runtime, corrupt libmpv, or an initialization option rejected by the core.","commonSituations":"User swapped libmpv for a mismatched build; missing runtime after a fresh Windows install; antivirus damaged libmpv; SE upgraded to a libmpv needing a newer runtime.","solutions":["Read the embedded GetErrorString(err) in the exception message for the concrete mpv error.","Confirm libmpv bitness/arch matches the process and install the required C++ runtime.","Re-download the mpv player through SE to replace a corrupt/mismatched libmpv.","Consider guarding LoadLib with CanLoad() and wrapping Initialize in try/catch that resets preview UI state, matching the pattern in ReviewSpeechViewModel."],"exampleFix":"// before\nplayer.LoadLib();\nvar err = player.Initialize();\nif (err < 0) throw new InvalidOperationException($\"Failed to initialize mpv: {player.GetErrorString(err)}\");\n\n// after - validate load and contain init failures so preview does not strand\nplayer.LoadLib();\nif (!player.CanLoad())\n    throw new InvalidOperationException(\"libmpv could not be loaded for audio preview.\");\nint err;\ntry { err = player.Initialize(); }\ncatch (Exception ex) { throw new InvalidOperationException(\"mpv init threw: \" + ex.Message, ex); }\nif (err < 0) throw new InvalidOperationException($\"Failed to initialize mpv: {player.GetErrorString(err)}\");","handlingStrategy":"try-catch","validationCode":"player = new LibMpvDynamicPlayer();\nif (!player.CanLoad()) throw new InvalidOperationException(\"libmpv could not be loaded for preview.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    lock (_playLock)\n    {\n        player = new LibMpvDynamicPlayer();\n        player.LoadLib();\n        var err = player.Initialize();\n        if (err < 0) throw new InvalidOperationException($\"Failed to initialize mpv: {player.GetErrorString(err)}\");\n        _mpvContext = player;\n    }\n    await player.LoadAudio(fileName);\n}\ncatch (Exception ex)\n{\n    SeLogger.Error(ex, \"TTS preview playback failed\");\n    // reset preview UI state and re-enable controls\n}","preventionTips":["Use CanLoad() to distinguish missing libmpv from init failure.","Re-check the _mpvContext null race on window close noted in the comment.","Reset preview UI in the catch so controls are not left disabled."],"tags":["mpv","libmpv","native","audio","tts","preview"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}