媒体提取器显示“未能实例化提取器”

时间:2012-10-01 11:00:17

标签: android android-layout android-intent

我正在尝试在Android上使用OGG文件格式的音频流媒体提取器。 我在谷歌文档的帮助下写了一些代码。但它根本不起作用。 可能是我写了错误的代码或语法因为我是学生。 它显示我无法实例化提取器

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource("http://examplelink.com/ogg");// I cant put real link so sorry for that
int numTracks = extractor.getTrackCount();
for (int i = 0; i < numTracks; ++i) {
    MediaFormat format = extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    extractor.selectTrack(i);

    ByteBuffer inputBuffer = ByteBuffer.allocate(1024);
    while (extractor.readSampleData(inputBuffer,0) >= 0) {
            int trackIndex = (int) extractor.getSampleTime();
            long presentationTimeUs = extractor.getSampleTime();
    }

    MediaCodec codec = MediaCodec.createDecoderByType(mime);
    codec.configure(format, null /* surface */, null /* crypto */, 0 /* flags */);
    codec.start();
    ByteBuffer[] inputBuffers = codec.getInputBuffers();
    ByteBuffer[] outputBuffers = codec.getOutputBuffers();
    format = codec.getOutputFormat();
    Long timeoutUs=(long) 1;
    for (;;) {
        int inputBufferIndex = codec.dequeueInputBuffer(timeoutUs);
        if (inputBufferIndex >= 0) {
            // fill inputBuffers[inputBufferIndex] with valid data
            codec.queueInputBuffer(inputBufferIndex, 0, 128, 0,0);
        }
        MediaCodec.BufferInfo info = new BufferInfo();
        int outputBufferIndex = codec.dequeueOutputBuffer(info, timeoutUs);
        if (outputBufferIndex >= 0) {
            // outputBuffer is ready to be processed or rendered.
            codec.releaseOutputBuffer(outputBufferIndex, false);
        } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
            outputBuffers = codec.getOutputBuffers();
        } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
            // Subsequent data will conform to new format.
            format = codec.getOutputFormat();
            AudioTrack mAudioTrack = null;
            mAudioTrack.setPlaybackRate(format.getInteger(MediaFormat.KEY_SAMPLE_RATE));
        }
        codec.stop();
        codec.release();
        codec = null;
    }
}

}

4 个答案:

答案 0 :(得分:2)

我认为你的问题是“为什么它根本不起作用?” 通常,这是一个太大的问题,但在这种情况下,它看起来像你根本不了解MediaExtractor&amp; MediaCodec工作。

虽然不是你的全部错误。文件很神秘,请善意。 但是我已经成功地以这种方式播放音频文件。

我的方案假设MediaCodec实现了缓冲区的异步队列。此队列中似乎有大约4个缓冲区。

所以我使用2个线程:一个线程将来自MediaExtractor的数据放入队列,另一个线程将解码后的音频从队列中取出并将其写入AudioTrack。

然后我需要非常小心,以避免在搜索过程中发生死锁。我最终得到的代码比你的代码多得多。我不知道如何避免这种情况!

我正在考虑尝试视频。任何有用的参考将不胜感激!

答案 1 :(得分:1)

代码如下所示,这对我来说非常适合JB平台的本地音频文件。

try{
  Extractor lExt = new MediaExtractor();
  lExt.setDataSource(file path); //I have tried with local file
  lExt.setTrack(0); //For local file only one track will be present
  MediaFormat format = lExt.getTrackFormat(0); //0 is the track ID
  String mime = format.getString(MediaFormat.KEY_MIME);
  MediaCodec codec = MediaCodec.createDecoderByType(mime);
  codec.configure(
       format,//format of input data ::decoder OR desired format of the output data:encoder
       null,//Specify a surface on which to render the output of this decoder
       null,//Specify a crypto object to facilitate secure decryption
       0 //For Decoding, encoding use: CONFIGURE_FLAG_ENCODE 
       );
  codec.start();
  codec.flush();

  //Get Input and Output buffers from codec
  inputBuffers = codec.getInputBuffers();
  outputBuffers = codec.getOutputBuffers(); 

  While(condition)
  {
      //Get Audio data from extractor
      int inputBufIndex = codec.dequeueInputBuffer(50);//Timeout set of 50 microsec
      if (inputBufIndex >= 0) 
      {
           ByteBuffer dstBuf = inputBuffers[inputBufIndex]; 
           int sampleSize = extractor.readSampleData(dstBuf, 0);
           long presentationTimeUs = extractor.getSampleTime();

           codec.queueInputBuffer(inputBufIndex,
                           0, //offset
                           sampleSize,
                           presentationTimeUs,
                           0); //Use appropriate flag value
           extractor.advance();
        }

      //Use codec to decode the data -- handle end of stream properly
        MediaCodec.BufferInfo info = new  MediaCodec.BufferInfo();
        info.set(0,0,0,0);
        final int res = codec.dequeueOutputBuffer(info, 20000);
        if (res >= 0) 
        {
            int outputBufIndex = res;
            ByteBuffer buf = outputBuffers[outputBufIndex];

            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) 
               //Use this chunk as it contains decoded audio dat            
            codec.releaseOutputBuffer(outputBufIndex, false /* render */); 
        } 
        else if (res == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) 
            outputBuffers = codec.getOutputBuffers();
  }
  codec.stop();
  codec.release();
  lExt.release();
}
catch(Exception ex)...

答案 2 :(得分:1)

将数据源设置为远程URL需要在AndroidManifest.xml中声明 android.permission.INTERNET 权限

<uses-permission android:name="android.permission.INTERNET" />

答案 3 :(得分:0)

尝试传递打开文件的文件描述符,而不是文件路径。当我遇到这个问题时,它对我有用。我不明白为什么。