{"record":{"id":"017f8d51888ed52e","repo":"stride3d/stride","slug":"the-media-codec-has-not-been-initialized-for-a-media","errorCode":null,"errorMessage":"The Media Codec has not been initialized for a media","messagePattern":"The Media Codec has not been initialized for a media","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Video/Android/MediaCodecExtractorBase.cs","lineNumber":289,"sourceCode":"            }\n            catch (Exception)\n            {\n                throw;\n            }\n            finally\n            {\n                Logger.Debug(\"Media Extraction done\");\n                Release();\n            }\n        }\n\n        private void ExtractMedia()\n        {\n            if (MediaDecoder == null)\n                throw new InvalidOperationException(\"The Media Codec Extractor has not been initialized\");\n\n            if (!isInitialized)\n                throw new InvalidOperationException(\"The Media Codec has not been initialized for a media\");\n            \n            var bufferInfo = new MediaCodec.BufferInfo();\n            var waitDefaultTime = TimeSpan.FromMilliseconds(10);\n\n            MediaDecoder.Start();\n            while (true)\n            {\n                var waitTime = waitDefaultTime; // time to wait at the end of the loop iteration\n\n                //Process the commands\n                if (ProcessCommandsAndUpdateCurrentState())\n                    waitTime = TimeSpan.Zero;\n\n                // terminate the thread on disposal\n                if (currentState == SchedulerAsyncCommandEnum.Dispose)\n                    return;\n\n                //=================================================================================================","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Video/Android/MediaCodecExtractorBase.cs#L271-L307","documentation":"Thrown by MediaCodecExtractorBase.ExtractMedia when the worker thread starts pulling samples from the MediaCodec decoder before Initialize(media) was successfully called (or it was disposed/reset). The extractor guards two preconditions: MediaDecoder created, and isInitialized set for a specific media; this error fires for the second one, meaning the codec exists but was never configured for the current media source.","triggerScenarios":"Calling ExtractMedia / starting the extraction worker without a prior successful Initialize() call for the media; calling Reset/Dispose and re-running extraction without re-initializing; a failed Initialize that left MediaDecoder non-null but isInitialized false.","commonSituations":"Reusing one extractor instance across multiple video files without re-initializing; races where the extraction thread starts before initialization completes; swallowing an exception from Initialize and continuing to play.","solutions":["Call Extractor.Initialize(media) (and wait for its success) before starting extraction","Check the return/exception of Initialize before starting the worker thread","Re-create or re-initialize the extractor after Reset/Dispose before reuse","Inspect thread startup ordering so ExtractMediaWorkerFunction cannot run before initialization completes"],"exampleFix":"// before\nextractor.Reset();\nextractor.ExtractMediaWorkerFunction(); // throws: not initialized for new media\n// after\nextractor.Reset();\nextractor.Initialize(newMedia);\nextractor.ExtractMediaWorkerFunction();","handlingStrategy":"validation","validationCode":"if (extractor == null || !extractor.IsInitialized)\n    throw new InvalidOperationException(\"Extractor must be initialized before extraction starts\");","typeGuard":"bool IsReadyForExtraction(MediaCodecExtractorBase e) => e is { MediaDecoder: not null } && e.IsInitialized;","tryCatchPattern":"try { extractor.ExtractMediaWorkerFunction(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"has not been initialized\"))\n{ /* re-initialize extractor and retry once */ }","preventionTips":["Always pair Reset/Dispose with a fresh Initialize","Await/verify Initialize completion before starting worker threads","Never swallow exceptions from Initialize"],"tags":["android","mediacodec","video-decoding","initialization"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}