I want to decode the mp3 using the MediaCodec. However, I do not want to use the MediaExtractor. Because it is because I want to get the meta data from mp3. icy (Shoutcast) stream are also taken into consideration.
I tried writing the code to decode the mp3 files from FileInputStream by improving the sample of MediaExtractor. However decode caused the error is not possible.
package com.test;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaFormat;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import com.test.R;
public class AndroidTestActivity extends Activity {
private static final String TAG = "TAG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread() {
@Override
public void run() {
try {
process();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
private synchronized void process() throws IOException {
File inputFile = new File(Environment.getExternalStorageDirectory().getPath() + "/download/music/test.mp3");
FileInputStream fis = new FileInputStream(inputFile);
MediaFormat format = MediaFormat.createAudioFormat("audio/mpeg", 44100,2);
String mime = format.getString(MediaFormat.KEY_MIME);
//int sampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
MediaCodec codec = MediaCodec.createDecoderByType(mime);
codec.configure(format, null , null , 0);
codec.start();
ByteBuffer[] codecInputBuffers = codec.getInputBuffers();
ByteBuffer[] codecOutputBuffers = codec.getOutputBuffers();
AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC,
format.getInteger(MediaFormat.KEY_SAMPLE_RATE),
AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
8000, AudioTrack.MODE_STREAM);
track.play();
BufferInfo info = new BufferInfo();
boolean inputEos = false;
boolean outputEos = false;
long timeoutUs = 1000;
byte[] tempBuffer = new byte[8192];
while (!outputEos) {
if (!inputEos) {
int inputBufIndex = codec.dequeueInputBuffer(timeoutUs);
if (inputBufIndex >= 0) {
ByteBuffer buf = codecInputBuffers[inputBufIndex];
int bytesRead = fis.read(tempBuffer, 0, buf.limit());
long presentationTimeUs = 0;
if (bytesRead = 0) {
int outputBufIndex = res;
ByteBuffer buf = codecOutputBuffers[outputBufIndex];
byte[] dst = new byte[info.size];
int oldPosition = buf.position();
buf.get(dst);
buf.position(oldPosition);
//Play music
track.write(dst, 0, dst.length);
codec.releaseOutputBuffer(outputBufIndex, false);
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
Log.d(TAG, "output eos.");
outputEos = true;
}
} else if (res == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
Log.d(TAG, "output buffer changed.");
codecOutputBuffers = codec.getOutputBuffers();
} else if (res == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
MediaFormat f = codec.getOutputFormat();
Log.d(TAG, "output format changed. " + f);
//track.setPlaybackRate(f.getInteger(MediaFormat.KEY_SAMPLE_RATE));
}
}
Log.d(TAG, "complete stop!");
fis.close();
codec.stop();
codec.release();
}
}
Error during execution...
01-20 00:57:20.436: I/System.out(2626): Sending WAIT chunk
01-20 00:57:20.436: W/ActivityThread(2626): Application com.test is waiting for the debugger on port 8100...
01-20 00:57:20.446: I/dalvikvm(2626): Debugger is active
01-20 00:57:20.636: I/System.out(2626): Debugger has connected
01-20 00:57:20.636: I/System.out(2626): waiting for debugger to settle...
01-20 00:57:20.836: I/System.out(2626): waiting for debugger to settle...
01-20 00:57:21.036: I/System.out(2626): waiting for debugger to settle...
01-20 00:57:21.236: I/System.out(2626): waiting for debugger to settle...
01-20 00:57:21.441: I/System.out(2626): waiting for debugger to settle...
01-20 00:57:21.641: I/System.out(2626): waiting for debugger to settle...
01-20 00:57:21.846: I/System.out(2626): waiting for debugger to settle...
01-20 00:57:22.046: I/System.out(2626): debugger has settled (1443)
01-20 00:57:22.271: I/OMXClient(2626): Using client-side OMX mux.
01-20 00:57:22.276: E/SEC_COMP_REGS(2626): .
01-20 00:57:22.276: E/SEC_COMP_REGS(2626): ..
01-20 00:57:22.276: E/SEC_COMP_REGS(2626): libOMX.SEC.AVC.Decoder.so
01-20 00:57:22.276: E/SEC_COMP_REGS(2626): Path & libName : /system/lib/omx/libOMX.SEC.AVC.Decoder.so
01-20 00:57:22.276: E/SEC_COMP_REGS(2626): libOMX.SEC.AVC.Encoder.so
01-20 00:57:22.276: E/SEC_COMP_REGS(2626): Path & libName : /system/lib/omx/libOMX.SEC.AVC.Encoder.so
01-20 00:57:22.281: E/SEC_COMP_REGS(2626): libOMX.SEC.M4V.Decoder.so
01-20 00:57:22.281: E/SEC_COMP_REGS(2626): Path & libName : /system/lib/omx/libOMX.SEC.M4V.Decoder.so
01-20 00:57:22.281: E/SEC_COMP_REGS(2626): libOMX.SEC.M4V.Encoder.so
01-20 00:57:22.291: E/SEC_COMP_REGS(2626): Path & libName : /system/lib/omx/libOMX.SEC.M4V.Encoder.so
01-20 00:57:22.291: E/SEC_COMP_REGS(2626): libOMX.SEC.WMV.Decoder.so
01-20 00:57:22.291: E/SEC_COMP_REGS(2626): Path & libName : /system/lib/omx/libOMX.SEC.WMV.Decoder.so
01-20 00:57:22.311: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.321: D/TAG(2626): output format changed. {sample-rate=44100, channel-count=2, what=1869968451, mime=audio/raw}
01-20 00:57:22.331: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.341: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.351: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.396: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.436: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.481: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.486: D/libEGL(2626): loaded /system/lib/egl/libEGL_mali.so
01-20 00:57:22.491: D/libEGL(2626): loaded /system/lib/egl/libGLESv1_CM_mali.so
01-20 00:57:22.496: D/libEGL(2626): loaded /system/lib/egl/libGLESv2_mali.so
01-20 00:57:22.506: E/(2626): Device driver API match
01-20 00:57:22.506: E/(2626): Device driver API version: 23
01-20 00:57:22.506: E/(2626): User space API version: 23
01-20 00:57:22.506: E/(2626): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 9 21:05:57 KST 2013
01-20 00:57:22.526: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.536: D/OpenGLRenderer(2626): Enabling debug mode 0
01-20 00:57:22.576: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.621: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.626: I/ActivityManager(2626): Timeline: Activity_idle id: android.os.BinderProxy@424d6780 time:611732103
01-20 00:57:22.666: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.716: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.811: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.856: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.901: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.951: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:22.996: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.041: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.091: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.136: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.226: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.276: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.321: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.366: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.416: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.461: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.506: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.551: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.656: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.691: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.741: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.786: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.831: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.876: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.926: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:23.976: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.066: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.111: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.156: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.201: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.256: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.296: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.341: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.386: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.481: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.526: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.576: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.621: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.666: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.711: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.756: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.801: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.896: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.941: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:24.986: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.036: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.081: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.126: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.176: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.221: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.316: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.361: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.411: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.456: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.501: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.546: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.596: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.641: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.736: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.781: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.831: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.876: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.921: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:25.966: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.016: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.061: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.151: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.201: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.246: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.296: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.336: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.386: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.436: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.476: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.571: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.621: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.666: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.711: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.756: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.831: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.876: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.911: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:26.986: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.031: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.076: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.126: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.176: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.221: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.266: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.311: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.406: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.451: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.501: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.551: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.591: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.636: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.686: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.731: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.826: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.871: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.916: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:27.966: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.011: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.056: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.101: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.151: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.241: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.291: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.336: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.381: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.426: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.476: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.521: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.566: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.661: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.706: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.756: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.801: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.856: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.891: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.941: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:28.986: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:29.076: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:29.126: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:29.171: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:29.221: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:29.266: E/SoftMP3(2626): mp3 decoder returned error 12
01-20 00:57:29.401: D/TAG(2626): output eos.
01-20 00:57:29.401: D/TAG(2626): complete stop!
01-20 00:57:29.411: I/OMXNodeInstance(2626): OMX_FreeBuffer for buffer header 0x524cc9d8 successful
01-20 00:57:29.411: I/OMXNodeInstance(2626): OMX_FreeBuffer for buffer header 0x524cc8d8 successful
01-20 00:57:29.411: I/OMXNodeInstance(2626): OMX_FreeBuffer for buffer header 0x524cc7c0 successful
01-20 00:57:29.416: I/OMXNodeInstance(2626): OMX_FreeBuffer for buffer header 0x524cad28 successful
01-20 00:57:29.416: I/OMXNodeInstance(2626): OMX_FreeBuffer for buffer header 0x524cd018 successful
01-20 00:57:29.421: I/OMXNodeInstance(2626): OMX_FreeBuffer for buffer header 0x524ccf48 successful
01-20 00:57:29.421: I/OMXNodeInstance(2626): OMX_FreeBuffer for buffer header 0x524cce30 successful
01-20 00:57:29.426: I/OMXNodeInstance(2626): OMX_FreeBuffer for buffer header 0x524cccb8 successful
来源:https://stackoverflow.com/questions/28029342/android-decode-mp3-with-mediacodec-not-use-mediaextractor