【问题标题】:Add some sound data to existing mp3 file Android,lame encoder将一些声音数据添加到现有的 mp3 文件 Android,lame 编码器
【发布时间】:2018-06-06 00:43:49
【问题描述】:

所以,这就是 mp3 在 android 中从 mic 编码到文件的方式:

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();
    }

以上一切正常,并提供一个新的 mp3 文件,可以从音频播放器正常播放

现在我刚刚从 InputStream 中读取该 mp3 文件并获取文件字节数组:

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);
        }
    }

那么如何将一些新的音频数据添加到现有的 mp3 文件中,或者是否有任何选项可以将旧字节 + 新字节编码到新的 mp3 文件中以连接在一起?

有关导致问题的部分在哪里的更多信息,请阅读评论是第二个代码。

换个说法:

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

那么,我们可以在不重新编码的情况下修改现有的 mp3 文件字节声音数据吗?

【问题讨论】:

    标签: java android android-studio mp3 lame


    【解决方案1】:

    试试这个:

    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

    【讨论】:

    • 但问题是我没有第二个 mp3 文件。它应该从随机字节生成可以播放的mp3文件。
    【解决方案2】:

    让我试着回答你的问题:

    那么如何将一些新的音频数据添加到现有的 mp3 文件中,或者可能存在 是将旧字节 + 新字节编码到新 mp3 文件以加入的任何选项 一起吗?

    如果“添加一些音频”是指在 mp3 中附加或预先添加其他音频数据,答案是肯定的,您可以在不重新编码的情况下做到这一点

    您将使用的课程是MediaMuxer 你可以在https://developer.android.com/reference/android/media/MediaMuxer找到有用的信息
    https://bigflake.com/mediacodec/

    【讨论】:

    • 我已经接受了,但无论如何它并不完整。谢谢
    猜你喜欢
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多