You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MediaCodec解码音频后,调用AudioTrack播放触发致命异常求助

Troubleshooting AudioTrack SIGSEGV Crash After MediaCodec Audio Decoding

First, let's unpack the errors you're seeing:

  • The storeMetaDataInBuffers failed w/ err -2147483648 is an OMX codec error (likely OMX_ErrorUndefined) that might hint at a minor misconfiguration in your audio codec setup, though you mentioned decoding completes successfully. The critical issue here is the SIGSEGV (segmentation fault), which means your app is trying to access invalid memory—almost always tied to how you're handling decoded audio buffers or configuring the AudioTrack.

Here are the most likely fixes to try, ordered by probability:

1. Verify AudioTrack Parameters Match Decoded Audio Format

This is the #1 cause of audio playback crashes. Even if decoding succeeds, mismatched parameters between your AudioTrack and the actual decoded audio data will lead to memory access errors.

  • Actionable steps:
    • After configuring your audio MediaCodec, extract the output format to get the exact specs of the decoded audio:
      MediaFormat outputFormat = audioCodec.getOutputFormat();
      int sampleRate = outputFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE);
      int channelCount = outputFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
      int audioFormat = outputFormat.getInteger(MediaFormat.KEY_PCM_ENCODING);
      
    • Initialize your AudioTrack using these exact values. For example:
      int bufferSize = AudioTrack.getMinBufferSize(
          sampleRate,
          (channelCount == 1) ? AudioFormat.CHANNEL_OUT_MONO : AudioFormat.CHANNEL_OUT_STEREO,
          audioFormat
      );
      AudioTrack audioTrack = new AudioTrack(
          AudioManager.STREAM_MUSIC,
          sampleRate,
          (channelCount == 1) ? AudioFormat.CHANNEL_OUT_MONO : AudioFormat.CHANNEL_OUT_STEREO,
          audioFormat,
          bufferSize,
          AudioTrack.MODE_STREAM
      );
      
    • Common mistakes to avoid: Using AudioFormat.ENCODING_PCM_16BIT when decoded data is 24-bit, or forcing mono playback for stereo audio.

2. Validate Decoded Buffer Handling

A SIGSEGV almost always means you're accessing memory outside valid bounds. Double-check how you're handling buffers from MediaCodec:

  • When dequeuing an output buffer, skip any non-valid indices (like INFO_OUTPUT_FORMAT_CHANGED or INFO_TRY_AGAIN_LATER).
  • Use the offset and size values from MediaCodec.BufferInfo—never assume the entire buffer is filled with valid audio data.
  • Example of correct buffer processing:
    int bufferIndex = audioCodec.dequeueOutputBuffer(bufferInfo, TIMEOUT_US);
    if (bufferIndex >= 0) {
        ByteBuffer buffer = audioCodec.getOutputBuffer(bufferIndex);
        if (buffer != null && bufferInfo.size > 0) {
            // Restrict buffer access to only valid data
            buffer.position(bufferInfo.offset);
            buffer.limit(bufferInfo.offset + bufferInfo.size);
            
            byte[] audioData = new byte[bufferInfo.size];
            buffer.get(audioData);
            
            // Write only the valid segment to AudioTrack
            audioTrack.write(audioData, 0, bufferInfo.size);
        }
        audioCodec.releaseOutputBuffer(bufferIndex, false);
    }
    
  • Avoid: Accessing buffer data outside the offset/size range, or using a buffer that's already been released by MediaCodec.

3. Fix MediaCodec Audio Configuration

While the storeMetaDataInBuffers error might not be the direct cause, it's worth ensuring your audio codec is set up correctly:

  • When configuring the audio MediaCodec, pass null for the Surface parameter (audio codecs don't use surfaces—we decode to buffers).
  • Confirm your input format matches the audio stream's codec parameters (e.g., AAC profile, sample rate, channel count).
  • Always call audioCodec.start() after configuring it, before dequeuing input buffers.

4. Check AudioTrack State Before Playback

Trying to write to an uninitialized or stopped AudioTrack can trigger crashes. Add a quick check before writing data:

if (audioTrack.getState() == AudioTrack.STATE_INITIALIZED && audioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
    audioTrack.write(audioData, 0, bufferInfo.size);
}

Don't forget to call audioTrack.play() before writing data when using MODE_STREAM.

5. Isolate the Issue with Test Data

To rule out codec-specific problems, try feeding raw PCM test data directly to your AudioTrack. If this works, the issue is definitely in how you're handling buffers from MediaCodec. If it still crashes, your AudioTrack setup is incorrect.


内容的提问来源于stack exchange,提问作者Bikash Sahani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:16:31