convert audio,mp3 file to string and vice versa

微笑、不失礼 提交于 2019-12-07 18:41:33

问题


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?

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


回答1:


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.

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).

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

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



回答2:


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

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



来源:https://stackoverflow.com/questions/7282014/convert-audio-mp3-file-to-string-and-vice-versa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!