{"record":{"id":"c24e1955d056c36d","repo":"SubtitleEdit/subtitleedit","slug":"failed-to-initialize-mpv-mpvcontext-geterrorstr-c24e19","errorCode":null,"errorMessage":"Failed to initialize mpv: {_mpvContext.GetErrorString(err)}","messagePattern":"Failed to initialize mpv: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ui/Features/Video/TextToSpeech/ReviewSpeech/ReviewSpeechViewModel.cs","lineNumber":324,"sourceCode":"                    MessageBoxIcon.Error);\n            }\n\n            return;\n        }\n\n        try\n        {\n            lock (_playLock)\n            {\n                _mpvContext?.Stop();\n                _mpvContext?.Dispose();\n\n                _mpvContext = new LibMpvDynamicPlayer();\n                _mpvContext.LoadLib();\n                var err = _mpvContext.Initialize();\n                if (err < 0)\n                {\n                    throw new InvalidOperationException($\"Failed to initialize mpv: {_mpvContext.GetErrorString(err)}\");\n                }\n            }\n\n            await _mpvContext.LoadAudio(fileName);\n        }\n        catch (Exception exception)\n        {\n            // An mpv load/init failure must not escape: the callers disable all rows before\n            // awaiting and only the playback timer re-enables them, so an unhandled throw\n            // leaves every play/regenerate button dead until the window is reopened.\n            SeLogger.Error(exception, $\"ReviewSpeech: unable to play audio file \\\"{fileName}\\\"\");\n            ResetPlaybackUiState();\n            if (Window != null)\n            {\n                await MessageBox.Show(\n                    Window,\n                    Se.Language.General.Error,\n                    \"Unable to play audio: \" + exception.Message,","sourceCodeStart":306,"sourceCodeEnd":342,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Features/Video/TextToSpeech/ReviewSpeech/ReviewSpeechViewModel.cs#L306-L342","documentation":"Thrown inside ReviewSpeechViewModel.PlayAudio when LibMpvDynamicPlayer.Initialize() returns a negative mpv error code while preparing an audio preview. Initialize() invokes the native mpv_initialize on the handle created by LoadLib(); a negative value means the mpv core could not be brought up. Critically, this site wraps the throw in try/catch: because callers disable every play/regenerate row before awaiting and only the playback timer re-enables them, an unhandled exception here would leave the whole dialog dead until it is reopened, so the catch logs via SeLogger, calls ResetPlaybackUiState(), and shows a MessageBox.","triggerScenarios":"LoadLib() located a libmpv binary but mpv_initialize failed: wrong-architecture/bitness libmpv (e.g. x86 dll in an x64 process), missing dependent runtime such as the Visual C++ redistributable, a corrupted libmpv, or an mpv option rejected during initialization.","commonSituations":"User installed/replaced libmpv manually with the wrong build; a Windows machine lacking the VC++ runtime; a portable build whose libmpv was quarantined by antivirus; SE updated to a libmpv build requiring a newer runtime.","solutions":["Read the SeLogger output — the message embeds mpv's GetErrorString(err) which names the concrete failure.","Verify libmpv (libmpv.dll / libmpv.dylib / libmpv.so) bitness/arch matches the running process (x64 on 64-bit).","Install the Visual C++ Redistributable on Windows (or the equivalent C++ runtime on the platform).","Use SE's built-in 'Download video player' to replace a suspect/corrupt libmpv with a known-good copy."],"exampleFix":"// before - assumes load always succeeds\n_mpvContext.LoadLib();\nvar err = _mpvContext.Initialize();\nif (err < 0) throw new InvalidOperationException($\"Failed to initialize mpv: {_mpvContext.GetErrorString(err)}\");\n\n// after - guard the load step so the error is actionable\n_mpvContext.LoadLib();\nif (!_mpvContext.CanLoad())\n    throw new InvalidOperationException(\"libmpv could not be loaded - check the player path/bitness.\");\nvar err = _mpvContext.Initialize();\nif (err < 0) throw new InvalidOperationException($\"Failed to initialize mpv: {_mpvContext.GetErrorString(err)}\");","handlingStrategy":"try-catch","validationCode":"_mpvContext = new LibMpvDynamicPlayer();\nif (!_mpvContext.CanLoad())\n{\n    SeLogger.Error(\"libmpv not found for audio preview\");\n    return; // or prompt to download the player\n}","typeGuard":null,"tryCatchPattern":"// Already the recommended pattern at this site: catch resets UI state.\ntry { /* LoadLib/Initialize/LoadAudio */ }\ncatch (Exception ex)\n{\n    SeLogger.Error(ex, $\"ReviewSpeech: unable to play \\\"{fileName}\\\"\");\n    ResetPlaybackUiState();\n    await MessageBox.Show(Window, Se.Language.General.Error, \"Unable to play audio: \" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);\n}","preventionTips":["Call CanLoad() before Initialize() to separate 'library missing' from 'init failed'.","Keep libmpv arch/bitness aligned with the process via SE's player download.","Ensure the C++ runtime required by libmpv is installed."],"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"}