调用MediaCodec.configure()时出现“ JNI错误:GetStringUTFChars接收到NULL jstring”

时间:2019-05-28 09:17:31

标签: java android java-native-interface mediacodec

我想从* .mp3文件中获取原始数据。我发现MediaCodec对我来说很好,但是我得到了

A/64.myproj: java_vm_ext.cc:542] JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring
    java_vm_ext.cc:542]     in call to GetStringUTFChars
    java_vm_ext.cc:542]     from void android.media.MediaCodec.native_configure(java.lang.String[], java.lang.Object[], android.view.Surface, android.media.MediaCrypto, android.os.IHwBinder, int)
    java_vm_ext.cc:542] "main" prio=5 tid=1 Runnable
    java_vm_ext.cc:542]   | group="main" sCount=0 dsCount=0 flags=0 obj=0x74071ee0 self=0xed774000
    java_vm_ext.cc:542]   | sysTid=1552 nice=-10 cgrp=default sched=0/0 handle=0xf2463494
    java_vm_ext.cc:542]   | state=R schedstat=( 993990291 519274343 1413 ) utm=70 stm=29 core=1 HZ=100
    java_vm_ext.cc:542]   | stack=0xff31c000-0xff31e000 stackSize=8MB

mediaCodec.configure(mediaFormat, null, null, 0)上。

代码如下:

public static void foo(String filepath) throws IOException {
    MediaExtractor mediaExtractor = new MediaExtractor();
    mediaExtractor.setDataSource(filepath);
    MediaFormat mediaFormat = mediaExtractor.getTrackFormat(0);
    mediaExtractor.release();
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(filepath);
    final long durationMs = Long.parseLong(mmr.extractMetadata(
            MediaMetadataRetriever.METADATA_KEY_DURATION));
    mmr.release();

    MediaCodecList mediaCodecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
    mediaFormat.setString(MediaFormat.KEY_FRAME_RATE, null);
    String codecName = mediaCodecList.findDecoderForFormat(mediaFormat);
    MediaCodec mediaCodec = MediaCodec.createByCodecName(codecName);
    mediaCodec.setCallback(new MediaCodec.Callback() {
        @Override
        public void onInputBufferAvailable(MediaCodec codec, int index) {
            File file = new File(filepath);
            int size = (int) file.length();
            byte[] bytes = new byte[size];
            ByteBuffer in = codec.getInputBuffer(index);
            try (BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file))) {
                buf.read(bytes, 0, bytes.length);
                in.put(bytes);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            codec.queueInputBuffer(
                    index,
                    0,
                    size,
                    durationMs * 1000L,
                    MediaCodec.BUFFER_FLAG_END_OF_STREAM);
        }

        @Override
        public void onOutputBufferAvailable(MediaCodec codec, int index, MediaCodec.BufferInfo info) {
            ByteBuffer out = codec.getOutputBuffer(index);
            short buf[] = out.asShortBuffer().array();
        }

        @Override
        public void onError(MediaCodec codec, MediaCodec.CodecException e) {}

        @Override
        public void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {}
    });
    mediaCodec.configure(mediaFormat, null, null, 0);
    mediaCodec.start();
}

the huge stack trace on pastebin

0 个答案:

没有答案