使用MediaCodec解码音频后,调用AudioTrack播放触发致命异常求助
First, let's unpack the errors you're seeing:
- The
storeMetaDataInBuffers failed w/ err -2147483648is an OMX codec error (likelyOMX_ErrorUndefined) that might hint at a minor misconfiguration in your audio codec setup, though you mentioned decoding completes successfully. The critical issue here is theSIGSEGV(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_16BITwhen decoded data is 24-bit, or forcing mono playback for stereo audio.
- After configuring your audio MediaCodec, extract the output format to get the exact specs of the decoded 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_CHANGEDorINFO_TRY_AGAIN_LATER). - Use the
offsetandsizevalues fromMediaCodec.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/sizerange, 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
nullfor 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

