google/ExoPlayer · critical · FlacDecoderException

Failed to initialize decoder

Error message

Failed to initialize decoder

What it means

Thrown by the FlacDecoderJni constructor when the native flacInit() call returns 0 (a NULL decoder context). Unlike the 'Failed to load decoder native libraries' error, the JNI library IS loaded, but the underlying native FLAC decoder object could not be created — almost always because the native code failed to allocate its decoder/stream-decoder state.

Source

Thrown at extensions/flac/src/main/java/com/google/android/exoplayer2/ext/flac/FlacDecoderJni.java:68

    }
  }

  private static final int TEMP_BUFFER_SIZE = 8192; // The same buffer size as libflac.

  private final long nativeDecoderContext;

  @Nullable private ByteBuffer byteBufferData;
  @Nullable private ExtractorInput extractorInput;
  @Nullable private byte[] tempBuffer;
  private boolean endOfExtractorInput;

  public FlacDecoderJni() throws FlacDecoderException {
    if (!FlacLibrary.isAvailable()) {
      throw new FlacDecoderException("Failed to load decoder native libraries.");
    }
    nativeDecoderContext = flacInit();
    if (nativeDecoderContext == 0) {
      throw new FlacDecoderException("Failed to initialize decoder");
    }
  }

  /**
   * Sets the data to be parsed.
   *
   * @param byteBufferData Source {@link ByteBuffer}.
   */
  public void setData(ByteBuffer byteBufferData) {
    this.byteBufferData = byteBufferData;
    this.extractorInput = null;
  }

  /**
   * Sets the data to be parsed.
   *
   * @param extractorInput Source {@link ExtractorInput}.
   */

View on GitHub (pinned to dd430f7053)

Solutions

  1. Check for decoder/codec instance leaks: make sure the player and renderers are released before creating new ones, and avoid constructing FlacDecoderJni repeatedly in a loop.
  2. Rebuild the FLAC extension ensuring the libflac version bundled matches the JNI wrapper (follow extensions/flac README exactly), then redeploy a clean build.
  3. Reproduce with a trivial FLAC file to rule out file-specific corruption; if the error is sporadic, capture native memory usage (adb shell dumpsys meminfo) around the failure.
  4. Migrate to androidx.media3, whose prebuilt FLAC support is continuously tested, if maintaining a custom NDK build is not required.
Defensive patterns

Strategy: try-catch

Try / catch

try { new FlacDecoderJni(); } catch (FlacDecoderException e) { if ("Failed to initialize decoder".equals(e.getMessage())) { release other decoders, retry once, else surface error; } }

Prevention

When it happens

Trigger: Calling new FlacDecoderJni() (or playing a FLAC stream through the extension renderer) when FLAC__stream_decoder_new() inside flacInit() returns NULL, typically due to native memory exhaustion or a corrupted/incompatible native build.

Common situations: Running on a low-memory device or in a process that has already allocated many decoders (e.g. rapid codec reinstantiation during seek or playlist transitions); a mismatched or corrupted libflacJNI.so built against a different libflac version; rare native heap fragmentation on older 32-bit devices.

Related errors


AI-assisted analysis of google/ExoPlayer@dd430f7053 (2026-08-14). Data as JSON: /api/errors/86c2fc6131ef7806. Report an issue: GitHub.