如何转换音频MP3档案到字符串,反之亦然? [英] How to Convert audio .mp3 file to String and vice versa?

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

问题描述

是否有可能一个音频的mp3文件转换为字符串数据为发送数据到服务器,
和服务器将一个字符串数据返回到我的应用程序,我想这些数据的MP3文件转换和播放音频。

Is it possible to convert an audio mp3 file to a string data for sending data to the server, and server will return a string data to my app i want to convert that data to mp3 file and play audio.

我用这code,将MP3文件字符串数据

I am using this code to convert mp3 file to string data

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;

}

我不知道如何从转换strind数据找回我的MP3文件。

i don't know how to get back my mp3 file from converted strind data.

如果可能的话怎么样?任何人帮助我​​。

If possible then how ? anybody help me.

或者这一要求任何其他解决方案告诉我:我想音频和视频文件传递到服务器和从服务器取回应用程序使用

Or any other solution for this requirement tell me: i want to pass audio and video files to server and get back from the server to use in app.

推荐答案

使用Base64.en codeToString()试试这个<一个href=\"http://developer.android.com/reference/android/util/Base64.html#en$c$cToString%28byte%5B%5D,%20int%29\" rel=\"nofollow\">http://developer.android.com/reference/android/util/Base64.html#en$c$cToString%28byte%5B%5D,%20int%29

use Base64.encodeToString() try this 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);
        }
    }



    }

}

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

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