将音频,mp3文件转换为字符串,反之亦然 [英] convert audio,mp3 file to string and vice versa

查看:583
本文介绍了将音频,mp3文件转换为字符串,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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或音频文件?

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

推荐答案

一个mp3文件,例如,所有文件都是简单的二进制编码数据,根据您用来查看该数据的方式,它们可以有不同的解释.

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

但是,如果您要询问如何从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.

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

这篇关于将音频,mp3文件转换为字符串,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆