尝试使用 Jelly Beans 新的低级媒体 API 构建"Hello, world!"媒体播放器活动



我正在尝试测试新的低级媒体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 找不到"和"OMX_getExtensionIndex失败"。也许有人在我尝试使用日志编码之前有一些想法。我正在运行带有 Jelly Bean 版本 4.1.2 的 AVD,但我无法从 Eclipse 进行调试,所以我只使用日志来捕获错误。

编辑11月16日:我忘记了明显的audioTrack.play()函数,但这会导致崩溃,所以它再次被注释掉。

在我的代码中,我在构造 AudioTrack 之后得到了 audioTrack.play(),它可以工作。您是否确保在设备上打开了音量?起初我犯了这个错误,显然什么也听不到。您还可以使用音频管理器调高音量。

最新更新