如何在Android中通过MediaCodec编码到AAC?

时间:2014-03-19 12:24:35

标签: android audio mediacodec

我想将文件从.mp3转换为AAC,并引用Spydroid APP的源代码。

它将音频数据从Microphone audio source转换为AAC

我尝试修改代码并将音频源从Microphone audio source更改为local mp3 file

但我不知道如何将原始音频推送到解码器......

有人可以帮助我并给我一些建议吗?

编码代码如下:

protected void encodeWithMediaCodec() throws IOException {

        final int bufferSize = AudioRecord.getMinBufferSize(mQuality.samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT)*2;

        ((AACLATMPacketizer)mPacketizer).setSamplingRate(mQuality.samplingRate);

        //The music file in my phone , and I want to push it to the 
        long file_size = 0;
        string path = "/storage/sdcard1/music/test.mp3"
        File audio = new File(path );

        mMediaCodec = MediaCodec.createEncoderByType("audio/mp4a-latm");
        MediaFormat format = new MediaFormat();
        format.setString(MediaFormat.KEY_MIME, "audio/mp4a-latm");
        format.setInteger(MediaFormat.KEY_BIT_RATE, mQuality.bitRate);
        format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
        format.setInteger(MediaFormat.KEY_SAMPLE_RATE, mQuality.samplingRate);
        format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
        format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, mBuffer_Size);
        mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        mMediaCodec.start();

        final MediaCodecInputStream inputStream = new MediaCodecInputStream(mMediaCodec);
        final ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers();

        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                int len = 0, bufferIndex = 0;
                try {
                    while (!Thread.interrupted()) {
                        bufferIndex = mMediaCodec.dequeueInputBuffer(10000);
                        if (bufferIndex>=0) {
                            inputBuffers[bufferIndex].clear();


                            len = audio.length();
                            if (len ==  AudioRecord.ERROR_INVALID_OPERATION || len == AudioRecord.ERROR_BAD_VALUE) {
                                Log.e(TAG,"An error occured with the AudioRecord API !");
                            } else {
                                Log.v(TAG,"Pushing raw audio to the decoder: len="+len+" bs: "+inputBuffers[bufferIndex].capacity());
                                mMediaCodec.queueInputBuffer(bufferIndex, 0, len, System.nanoTime()/1000, 0);
                            }
                        }
                    }
                } catch (RuntimeException e) {
                    e.printStackTrace();
                }
            }
        });

        mThread.start();

        // The packetizer encapsulates this stream in an RTP stream and send it over the network
        mPacketizer.setDestination(mDestination, mRtpPort, mRtcpPort);
        mPacketizer.setInputStream(inputStream);
        mPacketizer.start();

        mStreaming = true;

    }

它有如下错误日志:

V/AACStream(25015): Pushing raw audio to the decoder: len=11952883 bs: 4096
W/System.err(25015): java.lang.IllegalStateException
W/System.err(25015):    at android.media.MediaCodec.queueInputBuffer(Native Method)
W/System.err(25015):    at net.majorkernelpanic.streaming.audio.AACStream$1.run(AACStream.java:258)
W/System.err(25015):    at java.lang.Thread.run(Thread.java:856)
W/InputMethodManagerService(  574): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@42afeb98 attribute=null

代码的错误在

mMediaCodec.queueInputBuffer(bufferIndex, 0, len, System.nanoTime()/1000, 0);

我认为错误发生在len,但我不知道如何修复它......

似乎没有把音乐文件放到编码器......我有两个问题

Question 1:

如何将音频文件推送到MediaCodec

我在代码中遗漏了什么吗?

Question 2:

如何确保数据已编码为AAC ??

0 个答案:

没有答案