2

I want to convert the file from .mp3 to AAC and I reference the source code of the Spydroid APP.

It convert the audio data from Microphone audio source to AAC.

I try to modify the code and change the audio source from Microphone audio source to local mp3 file.

But I don't know how to pushing raw audio to the decoder...

Does somebody can help me and give me some suggestion ?

The code of encode is like the following:

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;

    }

It has error log like the following:

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

And the error of the code is at the

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

I think the error is at the len , but I don't know how to fix it...

And it seems does not put the music file to the encoder...And I have two question

Question 1:

How to pushing the audio file to the MediaCodec?

Did I missing something at the code ?

Question 2:

How to make sure the data has been encode to the AAC ??

Martin
  • 2,813
  • 11
  • 43
  • 66
  • Possibly relevant: http://stackoverflow.com/questions/22303837/android-mediaextractor-and-mp3-stream/ – fadden Mar 19 '14 at 14:49
  • At first glance, there are a few things wrong/missing in your code: (1) you can't send mp3 data to the aac encoder. It needs to be PCM data. In other words, you need to decode your mp3 file first, then re-encode the decoded data. (2) you're trying to send the entire file to the codec at once. The buffer is not big enough for that. (3) you appear to not actually be reading anything from your mp3 file, so the buffer doesn't actually contain any audio data. – marcone Mar 19 '14 at 15:04
  • @marcone Thanks for you , for the First you say. What format should I decode for the mp3 file and what format should I re-encode ? – Martin Mar 20 '14 at 02:11
  • @marcone I want to send live audio streaming to other device , can I send the mp3 file direct to other device without encode ? – Martin Mar 20 '14 at 02:36
  • @fadden It seems not relevant...Thank for you! – Martin Mar 20 '14 at 03:03

0 Answers0