stride3d/stride · error · InvalidOperationException

The Media Codec Extractor has not been initialized

Error message

The Media Codec Extractor has not been initialized

What it means

ExtractMedia is the worker that pulls encoded samples from the MediaExtractor into the decoder. It throws InvalidOperationException when MediaDecoder is null, i.e. the extractor was never given/created its codec, so extraction cannot proceed.

Solutions

  1. Ensure Initialize completed successfully before starting extraction (check isInitialized / await the init task).
  2. Handle Initialize failures (unreadable file, no track) by aborting playback instead of proceeding to extract.
  3. Create/assign a MediaCodec decoder before calling ExtractMedia if driving the extractor manually.
  4. Re-create the extractor instance rather than reusing one whose initialization failed.

Example fix

// before
extractor.Initialize(url, MediaType.Video);
extractor.ExtractMediaWorkerFunction(); // may run before init finished
// after
if (extractor.Initialize(url, MediaType.Video))
    extractor.ExtractMediaWorkerFunction();
Defensive patterns

Strategy: try-catch

Validate before calling

if (extractor.IsInitialized && extractor.MediaDecoder != null)
    extractor.ExtractMediaWorkerFunction();

Type guard

bool ReadyToExtract(MediaCodecExtractorBase e) => e.MediaDecoder != null && e.IsInitialized;

Try / catch

try { extractor.ExtractMediaWorkerFunction(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("not been initialized")) { /* abort playback or re-run Initialize */ }

Prevention

When it happens

Trigger: Calling ExtractMedia (directly or via ExtractMediaWorkerFunction) before Initialize created/configured MediaDecoder; Initialize failed earlier (e.g. unreadable file, no track) leaving the decoder null; manual use of the extractor without going through the media player pipeline.

Common situations: Starting playback before async initialization completes; swallowing an earlier Initialize exception and then calling Play/extract; reusing an extractor instance after failed setup.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/254e9fbba0b2eb9b. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Video/Android/MediaCodecExtractorBase.cs:286

            try
            {
                ExtractMedia();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                Logger.Debug("Media Extraction done");
                Release();
            }
        }

        private void ExtractMedia()
        {
            if (MediaDecoder == null)
                throw new InvalidOperationException("The Media Codec Extractor has not been initialized");

            if (!isInitialized)
                throw new InvalidOperationException("The Media Codec has not been initialized for a media");
            
            var bufferInfo = new MediaCodec.BufferInfo();
            var waitDefaultTime = TimeSpan.FromMilliseconds(10);

            MediaDecoder.Start();
            while (true)
            {
                var waitTime = waitDefaultTime; // time to wait at the end of the loop iteration

                //Process the commands
                if (ProcessCommandsAndUpdateCurrentState())
                    waitTime = TimeSpan.Zero;

                // terminate the thread on disposal
                if (currentState == SchedulerAsyncCommandEnum.Dispose)

View on GitHub (pinned to 96fad776d2)