我正在尝试测试新的低级媒体 API 功能、MediaExtractor 和 MediaCodec。我正在遵循本指南:
http://dpsm.wordpress.com/2012/07/28/android-mediacodec-decoded/
我把这个函数放在一起,其中songsList.get(songIndex).get("songPath")
是另一个函数给出的 mp3 文件的路径。
/**
* Attempts at API level 16 implementation
*
* MediaExtractor, MediaCodec and AudioTrack to play audiofiles
*
* */
public void JBPlay(int songIndex) {
String LOG_TAG="JB";
//AssetFileDescriptor sampleFD = getResources().openRawResourceFd(R.raw.sample);
//AssetFileDescriptor sampleFD = getAssets().openFd(songsList.get(songIndex).get("songPath"));
// getResources().openRawResourceFD(songsList.get(songIndex).get("songPath"));
//Log.d("FD: ", sampleFD.toString());
MediaExtractor extractor;
MediaCodec codec;
ByteBuffer[] codecInputBuffers;
ByteBuffer[] codecOutputBuffers;
extractor = new MediaExtractor();
extractor.setDataSource(songsList.get(songIndex).get("songPath"));
//extractor.setDataSource(sampleFD.getFileDescriptor(),
// sampleFD.getStartOffset(), sampleFD.getLength());
Log.d(LOG_TAG, String.format("TRACKS #: %d", extractor.getTrackCount()));
MediaFormat format = extractor.getTrackFormat(0);
String mime = format.getString(MediaFormat.KEY_MIME);
Log.d(LOG_TAG, String.format("MIME TYPE: %s", mime));
codec = MediaCodec.createDecoderByType(mime);
codec.configure(format, null /* surface */, null /* crypto */, 0 /* flags */);
codec.start();
codecInputBuffers = codec.getInputBuffers();
codecOutputBuffers = codec.getOutputBuffers();
extractor.selectTrack(0); // <= You must select a track. You will read samples from the media from this track!
boolean sawInputEOS=false;
int inputBufIndex = codec.dequeueInputBuffer(1000);//1 second timeout ???
if (inputBufIndex >= 0) {
ByteBuffer dstBuf = codecInputBuffers[inputBufIndex];
int sampleSize = extractor.readSampleData(dstBuf, 0);
long presentationTimeUs = 0;
if (sampleSize < 0) {
sawInputEOS = true;
sampleSize = 0;
} else {
presentationTimeUs = extractor.getSampleTime();
}
codec.queueInputBuffer(inputBufIndex,
0, //offset
sampleSize,
presentationTimeUs,0);
//sawInputEOS ? MediaCodec.BUFFER_FLAG_END_OF_STREAM : 0);
if (!sawInputEOS) {
extractor.advance();
}
}
AudioTrack audioTrack = null;
BufferInfo info = null;
Boolean sawOutputEOS=false;
final int res = codec.dequeueOutputBuffer(info, 1000);
if (res >= 0) {
int outputBufIndex = res;
ByteBuffer buf = codecOutputBuffers[outputBufIndex];
final byte[] chunk = new byte[info.size];
buf.get(chunk); // Read the buffer all at once
buf.clear(); // ** MUST DO!!! OTHERWISE THE NEXT TIME YOU GET THIS SAME BUFFER BAD THINGS WILL HAPPEN
if (chunk.length > 0) {
audioTrack.write(chunk, 0, chunk.length);
}
codec.releaseOutputBuffer(outputBufIndex, false /* render */);
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
sawOutputEOS = true;
}
} else if (res == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
codecOutputBuffers = codec.getOutputBuffers();
} else if (res == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
final MediaFormat oformat = codec.getOutputFormat();
Log.d(LOG_TAG, "Output format has changed to " + oformat);
audioTrack.setPlaybackRate(oformat.getInteger(MediaFormat.KEY_SAMPLE_RATE));
}
// EDIT nov. 16: addition of play() causes crash!
//audioTrack.play()
}
它似乎没有抛出任何重大错误,但没有声音,并且在日志中有一条消息“libwvm.so not found”和“OMX_getExtensionIndex failed”。在我尝试使用日志编写代码之前,也许有人有一些想法。我正在使用 Jelly Bean 版本 4.1.2 运行 AVD,我无法从 Eclipse 进行调试,所以我只使用日志来捕获错误。
编辑十一月 16:我忘记了明显的 audioTrack.play() 函数,但这会导致崩溃,因此再次将其注释掉。