如何将音频.mp3文件转换为字符串,反之亦然?

Raj*_*ddy 1 android audio-streaming

是否可以将音频mp3文件转换为字符串数据以将数据发送到服务器,服务器将字符串数据返回到我的应用程序,我想将该数据转换为mp3文件并播放音频。

我正在使用此代码将mp3文件转换为字符串数据

public static String readFileAsString(String filePath) throws java.io.IOException {

    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    String line, results = "";
    while( ( line = reader.readLine() ) != null)
    {
        results += line;
    }
    reader.close();
    return results;

}
Run Code Online (Sandbox Code Playgroud)

我不知道如何从转换后的strind数据取回我的mp3文件。

如果可能的话怎么办?有人帮我

或任何其他解决方案,告诉我:我想将音频和视频文件传递到服务器,然后从服务器取回以在应用程序中使用。

Nip*_*gia 5

使用Base64.encodeToString()试试这个http://developer.android.com/reference/android/util/Base64.html#encodeToString%28byte%5B%5D,%20int%29

File file = new File(Environment.getExternalStorageDirectory() + "/hello-4.wav");
byte[] bytes = FileUtils.readFileToByteArray(file);

String encoded = Base64.encodeToString(bytes, 0);                                       
Utilities.log("~~~~~~~~ Encoded: ", encoded);

byte[] decoded = Base64.decode(encoded, 0);
Utilities.log("~~~~~~~~ Decoded: ", Arrays.toString(decoded));

try
{
    File file2 = new File(Environment.getExternalStorageDirectory() + "/hello-5.wav");
    FileOutputStream os = new FileOutputStream(file2, true);
    os.write(decoded);
    os.close();
}
catch (Exception e)
{
    e.printStackTrace();
}



public class FileUtils {

    /**
     * Instances should NOT be constructed in standard programming.
     */
    public FileUtils() { }

    /**
     * The number of bytes in a kilobyte.
     */
    public static final long ONE_KB = 1024;

    /**
     * The number of bytes in a megabyte.
     */
    public static final long ONE_MB = ONE_KB * ONE_KB;

    /**
     * The number of bytes in a gigabyte.
     */
    public static final long ONE_GB = ONE_KB * ONE_MB;



    public static String readFileToString(
            File file, String encoding) throws IOException {
        InputStream in = new java.io.FileInputStream(file);
        try {
            return IOUtils.toString(in, encoding);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }



    }

}
Run Code Online (Sandbox Code Playgroud)