{"record":{"id":"29a86d2bf05a14ad","repo":"SubtitleEdit/subtitleedit","slug":"failed-to-initialize-mpv-mpvcontext-geterrorstr-29a86d","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/ReviewSpeechHistory/ReviewSpeechHistoryViewModel.cs","lineNumber":113,"sourceCode":"        {\n            row.IsPlaying = false;\n            row.IsPlayingEnabled = true;\n        }\n    }\n\n    private async Task PlayAudio(string fileName)\n    {\n        lock (_playLock)\n        {\n            _mpvContext?.Stop();\n            _mpvContext?.Dispose();\n\n            _mpvContext = new LibMpvDynamicPlayer();\n            _mpvContext.LoadLib(); // core not initialized\"\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        _timer.Start();\n    }\n\n    [RelayCommand]\n    private void Ok()\n    {\n        OkPressed = true;\n        Window?.Close();\n    }\n\n    [RelayCommand]\n    private void Cancel()\n    {","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Features/Video/TextToSpeech/ReviewSpeechHistory/ReviewSpeechHistoryViewModel.cs#L95-L131","documentation":"Thrown inside ReviewSpeechHistoryViewModel.PlayAudio when LibMpvDynamicPlayer.Initialize() returns a negative mpv error code while preparing a history-entry audio preview. Same root cause shape as the Review Speech dialog, but this PlayAudio (lines 101-120) has NO try/catch around the lock/Initialize block — unlike error 301 — so the InvalidOperationException propagates to the caller (PlayItem). The history dialog relies on the caller or a higher-level handler to absorb it; otherwise playback state can be left inconsistent.","triggerScenarios":"LoadLib() found libmpv but mpv_initialize failed: wrong-bitness/arch libmpv, missing C++ runtime dependency, corrupt libmpv, or an init-time option rejected by mpv. Distinct from a missing file (the caller guards missing FileName/File.Exists before reaching here).","commonSituations":"Same libmpv misconfiguration as 301 but surfaced through the Review Speech History window; a machine where libmpv loads but cannot initialize (e.g. sandboxed without GPU/render access, missing runtime).","solutions":["Check the SeLogger / application log for the GetErrorString(err) text embedded in the message.","Confirm libmpv arch/bitness matches the process and the C++ runtime is installed.","Re-fetch libmpv via SE's player download.","Wrap this PlayAudio in the same try/catch + UI-reset pattern already used in ReviewSpeechViewModel (error 301) so the history rows do not get stranded."],"exampleFix":"// before - exception escapes, can leave history rows disabled\nlock (_playLock)\n{\n    _mpvContext = new LibMpvDynamicPlayer();\n    _mpvContext.LoadLib();\n    var err = _mpvContext.Initialize();\n    if (err < 0) throw new InvalidOperationException($\"Failed to initialize mpv: {_mpvContext.GetErrorString(err)}\");\n}\nawait _mpvContext.LoadAudio(fileName);\n\n// after - mirror the defensive catch from ReviewSpeechViewModel\ntry\n{\n    lock (_playLock) { /* ...init as above... */ }\n    await _mpvContext.LoadAudio(fileName);\n}\ncatch (Exception ex)\n{\n    SeLogger.Error(ex, $\"ReviewSpeechHistory: unable to play \\\"{fileName}\\\"\");\n    ResetPlaybackUiState();\n}","handlingStrategy":"try-catch","validationCode":"_mpvContext = new LibMpvDynamicPlayer();\nif (!_mpvContext.CanLoad()) { SeLogger.Error(\"libmpv not found (history preview)\"); return; }","typeGuard":null,"tryCatchPattern":"// This site currently lacks a local catch - add one mirroring ReviewSpeechViewModel:\ntry\n{\n    lock (_playLock) { /* init as above */ }\n    await _mpvContext.LoadAudio(fileName);\n}\ncatch (Exception ex)\n{\n    SeLogger.Error(ex, $\"ReviewSpeechHistory: unable to play \\\"{fileName}\\\"\");\n    ResetPlaybackUiState();\n}","preventionTips":["Wrap PlayAudio the same way ReviewSpeechViewModel does so rows are never stranded.","Guard missing files before calling (the caller already does File.Exists).","Validate libmpv loads before attempting Initialize."],"tags":["mpv","libmpv","native","audio","tts","history"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}