java 将音频、mp3 文件转换为字符串,反之亦然

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7282014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:23:54  来源:igfitidea点击:

convert audio,mp3 file to string and vice versa

javaaudiomp3

提问by Pratik

Is it possible to convert an audio mp3 file to a string file, or read the mp3 file and write in a text file and vice versa?

是否可以将音频 mp3 文件转换为字符串文件,或读取 mp3 文件并写入文本文件,反之亦然?

If possible then how? Also, would the text file size be greater or equal to the original mp3 or audio file?

如果可能,那怎么办?另外,文本文件大小会大于或等于原始 mp3 或音频文件吗?

采纳答案by Zeeno

An mp3 file like, all files is simply binary encoded data which can be interpreted differently depending on what you use to view that data.

像所有文件一样的 mp3 文件只是二进制编码的数据,可以根据您用于查看该数据的内容进行不同的解释。

If you're asking if you can convert an audio file to a string then that is an incorrect question as binary data is only viewed as a string when given a character-encoding (you could open your mp3 file in notepad and 'read' it if you wanted).

如果您问是否可以将音频文件转换为字符串,那么这是一个不正确的问题,因为二进制数据仅在给定字符编码时才被视为字符串(您可以在记事本中打开您的 mp3 文件并“读取”它如果你愿意)。

However, if you're asking how you can read from an mp3 file and write to another file then this is code for it.

但是,如果您要问如何从 mp3 文件读取并写入另一个文件,那么这就是它的代码。

public String readFile(String filename) {

    // variable representing a line of data in the mp3 file
    String line = "";

    try {
        br = new BufferedReader(new FileReader(new File(filename)));

        while (br.readLine() != null) {
          line+=br.readLine

        try {
            if (br == null) {

                // close reader when all data is read
                br.close();
            }

        } catch (FileNotFoundException e) {
            e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (FileNotFoundException e) {
        e.getMessage();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Then to write to another file by calling the file reader method in the second file.

然后通过调用第二个文件中的文件读取器方法写入另一个文件。

public void outputData(String outputFile) {

            try {

        // Create file
        FileWriter fileStream = new FileWriter(outputFile);
        BufferedWriter writer = new BufferedWriter(fileStream);

        writer.write(readFile(THE MP3 FILE DIRECTORY));

        // Close writer
        writer.close();

        // Handle exceptions
    } catch (Exception e) {
        e.printStackTrace();
    }
}

回答by Mister Smith

Files are bytes, which of course can be interpreted as characters in a given encoding.

文件是字节,当然可以将其解释为给定编码中的字符。

I'd suggest to use Base64encoding here. In this case the output file will be 33% bigger.

我建议在Base64这里使用编码。在这种情况下,输出文件将大 33%。