Audio File Encoding and Decoding

2019-07-20 22:17发布

问题:

What is the efficient way to encode and decode an audio file in android I have tried the Base64 as below but after decoding the file size get increased. Encode

 ByteArrayOutputStream objByteArrayOS = new ByteArrayOutputStream();
        File file = new File(path);
        FileInputStream objFileIS;
        try {
            objFileIS = new FileInputStream(file);
            byte[] byteBufferString = new byte[1024];

        for (int readNum; (readNum = objFileIS.read(byteBufferString)) != -1;) {
            objByteArrayOS.write(byteBufferString, 0, readNum);
            System.out.println("read " + readNum + " bytes,");
            byte[] bytes = FileUtils.readFileToByteArray(file);


            strAttachmentCoded = Base64.encodeToString(bytes,
                    Base64.DEFAULT);

Decode

byte[] decoded = Base64.decode(strAttachmentCoded,
                    Base64.DEFAULT);
            // byte[] decoded1 = Base64.decode(byteBinaryData1, 0);
            File file1 = new File(pathAudio);

            FileOutputStream os = new FileOutputStream(file1, true);
            os.write(decoded);

            os.close();

i want audio in String format to send to server and retrieve the same as per the user requirement.

回答1:

wikipedia:base64 (rfc3548) is the right method to choose I would think. It is most common now I think having taken over from wikipedia:uuencoding.

To answer the question . . .

  1. You could add some padding. The wikipedia article on base64 gives a good example of padding.
  2. Or you could add a header to your audio string including length. The header could also include other control data so it may be something you want to include anyway.