Android Base64 音频文件编码/解码
Android Base64 Audio File Encode / Decode
正在做:我目前正在录制语音并将其作为文件保存在 sdCard 中,运行 在 MediaPlayer 中播放正常。
我想要的: 当我将这个文件编码成Base64并发送到服务器时,一切正常。但是当我将 Base64 字符串解码为 audio.m4a 文件时,它在 MediaPlayer 中不是 运行。
我试过 .m4a , .wav 但都是徒劳。
问题出在编码上。因为当我解码从同一个 iOS 应用程序发送的文件时,它在 MediaPlayer 中运行良好。
我知道它非常基本,并且有很多帮助可以编码解码,但没有任何效果。以下是我的方法:
private void encodeAudio(String selectedPath) {
byte[] audioBytes;
try {
// Just to check file size.. Its is correct i-e; Not Zero
File audioFile = new File(selectedPath);
long fileSize = audioFile.length();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(new File(selectedPath));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
audioBytes = baos.toByteArray();
// Here goes the Base64 string
_audioBase64 = Base64.encodeToString(audioBytes, Base64.DEFAULT);
} catch (Exception e) {
DiagnosticHelper.writeException(e);
}
}
并按以下方式解码:
private void decodeAudio(String base64AudioData, File fileName, String path, MediaPlayer mp) {
try {
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(Base64.decode(base64AudioData.getBytes(), Base64.DEFAULT));
fos.close();
try {
mp = new MediaPlayer();
mp.setDataSource(path);
mp.prepare();
mp.start();
} catch (Exception e) {
DiagnosticHelper.writeException(e);
}
} catch (Exception e) {
e.printStackTrace();
}
}
如果我做错了什么请指出。
谢谢
问题是在发布音频之前我正在将音频转换为 Base64 String
这就是为什么 解码 文件已损坏!!干杯
正在做:我目前正在录制语音并将其作为文件保存在 sdCard 中,运行 在 MediaPlayer 中播放正常。
我想要的: 当我将这个文件编码成Base64并发送到服务器时,一切正常。但是当我将 Base64 字符串解码为 audio.m4a 文件时,它在 MediaPlayer 中不是 运行。
我试过 .m4a , .wav 但都是徒劳。 问题出在编码上。因为当我解码从同一个 iOS 应用程序发送的文件时,它在 MediaPlayer 中运行良好。
我知道它非常基本,并且有很多帮助可以编码解码,但没有任何效果。以下是我的方法:
private void encodeAudio(String selectedPath) {
byte[] audioBytes;
try {
// Just to check file size.. Its is correct i-e; Not Zero
File audioFile = new File(selectedPath);
long fileSize = audioFile.length();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(new File(selectedPath));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
audioBytes = baos.toByteArray();
// Here goes the Base64 string
_audioBase64 = Base64.encodeToString(audioBytes, Base64.DEFAULT);
} catch (Exception e) {
DiagnosticHelper.writeException(e);
}
}
并按以下方式解码:
private void decodeAudio(String base64AudioData, File fileName, String path, MediaPlayer mp) {
try {
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(Base64.decode(base64AudioData.getBytes(), Base64.DEFAULT));
fos.close();
try {
mp = new MediaPlayer();
mp.setDataSource(path);
mp.prepare();
mp.start();
} catch (Exception e) {
DiagnosticHelper.writeException(e);
}
} catch (Exception e) {
e.printStackTrace();
}
}
如果我做错了什么请指出。 谢谢
问题是在发布音频之前我正在将音频转换为 Base64 String
这就是为什么 解码 文件已损坏!!干杯