Add some sound data to existing mp3 file Android,lame encoder
Asked Answered
R

2

6

So, this is how mp3 is encoded from mic to file in android :

private void startBufferedWrite(final File file) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                output = null;
                try {
                    output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
                    while (mIsRecording) {
                        int readSize = mRecorder.read(buffer, 0, buffer.length);
                        if (readSize > 0) {
                            int bytesEncoded = androidLame.encode(buffer, buffer, readSize, mp3buffer);
                            if (bytesEncoded > 0) {
                                try {
                                    output.write(mp3buffer, 0, bytesEncoded);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    Log.e("Error writing file : ", e.getMessage());
                }
            }
        }).start();
    }

Everything above is working correctly and providing a new mp3 file which is played ok from audio players

Now i just read that mp3 file from InputStream and get the file bytes array :

private void old_mp3_to_new_mp3(final File mp3, final File mp3_new) throws IOException {
        try {
            int size = 4;
            byte[] rawData = new byte[(int) mp3.length()];
            RandomAccessFile input = new RandomAccessFile(mp3, "rw");
            byte[] header = new byte[size];
            input.read(header, 0, size);
            //noinspection ResultOfMethodCallIgnored
            input.read(rawData);
            input.close();
            byte[] bytes = new byte[100000]; //create random bytes to write to mp3 as Random audio ???
            new Random().nextBytes(bytes);
            FileOutputStream output = new FileOutputStream(mp3_encrypted);
            FileChannel channel = output.getChannel();
            channel.write(ByteBuffer.wrap(header));
            channel.write(ByteBuffer.wrap(rawData));
            channel.write(ByteBuffer.wrap(bytes)); // if comment this line the mp3 new file is generated ok but it's the same with recording, but if wanted to write new random bytes the mp3 is generated but the file says that is corrupted...
            output.close();
            //noinspection ResultOfMethodCallIgnored
            mp3.delete();
            callbackListener.perfundoi_shkrimi_dhe_enkriptimi(mp3_encrypted);
        } catch (Exception ignored) {
            callbackListener.perfundoi_shkrimi_dhe_enkriptimi(mp3_encrypted);
        }
    }

So how to add some new audio data to existing mp3 file, or maybe there is any options to encode old bytes + new bytes to new mp3 file to join together ?

For more info where is the part that cause problems please read the comment is the second code.

To explain in other way :

mic -> mp3 ; mp3 file -> bytes + new_bytes -> mp3;

So, can we modify existing mp3 file byte sound data without re-encoding it ?

Rigid answered 6/6, 2018 at 0:43 Comment(0)
A
0

Let me try to answer your question:

So how to add some new audio data to existing mp3 file, or maybe there is any options to encode old bytes + new bytes to new mp3 file to join together ?

if by "add some audio" you mean append or prepend the mp3 with other audio data, the answer is YES, you can do it without reencode

The class you will use is MediaMuxer you can find usefull info at https://developer.android.com/reference/android/media/MediaMuxer
https://bigflake.com/mediacodec/

Azikiwe answered 15/6, 2018 at 22:50 Comment(1)
I have accepted it, but anyway it's not complete. Thank youRigid
H
1

Try this:

String wavFile1 = "C:\\1.mp3";
String wavFile2 = "C:\\2.mp3";
FileInputStream fistream1 = new FileInputStream(wavFile1);  // first source file
FileInputStream fistream2 = new FileInputStream(wavFile2);//second source file
SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
FileOutputStream fostream = new FileOutputStream("D://merge1.mp3");//destinationfile

int temp;

while( ( temp = sistream.read() ) != -1)
{
    // System.out.print( (char) temp ); // to print at DOS prompt
    fostream.write(temp);   // to write to file
}
fostream.close();
sistream.close();
fistream1.close();
fistream2.close();

Reference

Hodges answered 13/6, 2018 at 21:22 Comment(1)
But the problem is that i don't have the second mp3 file. it should be generated from random bytes to mp3 file which can be played.Rigid
A
0

Let me try to answer your question:

So how to add some new audio data to existing mp3 file, or maybe there is any options to encode old bytes + new bytes to new mp3 file to join together ?

if by "add some audio" you mean append or prepend the mp3 with other audio data, the answer is YES, you can do it without reencode

The class you will use is MediaMuxer you can find usefull info at https://developer.android.com/reference/android/media/MediaMuxer
https://bigflake.com/mediacodec/

Azikiwe answered 15/6, 2018 at 22:50 Comment(1)
I have accepted it, but anyway it's not complete. Thank youRigid

© 2022 - 2024 — McMap. All rights reserved.