音频文件编码base64 Android

时间:2019-09-30 17:05:08

标签: android base64

我有一个问题,我如何将音频文件发送到服务器,我曾尝试将其转换为base64,但没有任何效果。这是我的代码

变量声明:

private MediaRecorder grabacion;
private String archivoSalida = null;  

通过媒体记录获得的音频:

archivoSalida = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Myrecord.mp3";
        grabacion = new MediaRecorder();
        grabacion.setAudioSource(MediaRecorder.AudioSource.MIC);
        grabacion.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        grabacion.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        grabacion.setOutputFile(archivoSalida);

转换为base64,错误出现在这里:

private String convertirAudString(MediaRecorder audio){
   ByteArrayOutputStream array=new ByteArrayOutputStream();
    byte[] audioByte = new byte[(int)audio.length()];//error in this line

    String audioString = Base64.encodeToString(audioByte,Base64.DEFAULT);

    return audioString;
}

1 个答案:

答案 0 :(得分:0)

感谢您的所有建议。 我用解决 File file = new File(Environment.getExternalStorageDirectory() + "/Miaudio.mp3"); byte[] bytes = new byte[0]; bytes = FileUtils.readFileToByteArray(file);

然后我将其转换为base64;它对我非常快速。

相关问题