android base64.encode 参数,Android Base64音频文件编码/解码(Android Base64 Audio File Encode / Decode)...

长孙阳州
2023-12-01

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

}

}

请指出我是否做错了什么。 谢谢

Doing: I am currently recording voice and saving it as a file in sdCard which is running playing fine in MediaPlayer.

What i want: When i encode this file intoBase64 and send to server, everything goes fine. But When i decode the Base64 String into audio.m4a file, it is not running in MediaPlayer.

I had tried .m4a , .wav but all in vain. The problem is in encoding. Because when i decode a file sent from the same iOS app, it runs fine in MediaPlayer.

I know its very basic and alot of help is there to encode decode but nothing is working. Following is my approach:

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

}

}

And Decoding in the following way:

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

}

}

Please point if i am doing anything wrong. Thanks

原文:https://stackoverflow.com/questions/36307464

更新时间:2021-01-10 13:01

最满意答案

问题是在我将音频转换成Base64 String之前,重新传输音频,这就是为什么解码文件已损坏! 干杯

The problem is before relasing audio i was converting audio into Base64 String thats why Decoded file is corrupt !! Cheers

2016-04-12

相关问答

所以我正在回答自己 - 对此感到遗憾 - 但我认为这对于像我一样迷路的人可能是有用的;) 所以你必须使用ArrayBuffer ,并将你的XMLHttpRequest对象实例的responseType属性设置为arraybuffer以检索Bytes的本地数组,可以使用以下方便的函数将其转换为base64(在这里找到,作者可能会被祝福): function base64ArrayBuffer(arrayBuffer) {

var base64 = ''

var encodings =

...

问题是在我将音频转换成Base64 String之前,重新传输音频,这就是为什么解码文件已损坏! 干杯 The problem is before relasing audio i was converting audio into Base64 String thats why Decoded file is corrupt !! Cheers

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)

{

ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();

image.compress(compressFormat, quality, byteArrayOS);

return Base64.enc

...

如果我是对的,本地存储大小通常限制为5MB。 我建议使用Cordova File API http://docs.phonegap.com/en/3.0.0/cordova_file_file.md.html#File 我不确定你的建筑是什么类型的应用程序,但如果你使用5MB的本地存储和128kb / s的记录,就像40秒。 If I am right the localstorage size is usually limit to something like 5MB. I'd recomme

...

好吧,如果您使用Apache HTTP客户端连接到该服务。 我建议使用MultiPartEntity提交图像。 我不确定究竟是为什么,但是当我尝试使用Base64时,我遇到的问题是图像很大(OOM异常)。 使用Apache HTTP客户端提交映像。 您需要做的就是创建一个MultiPartEntity并使用addPart添加图像。 public class UploadImagePayload extends MultiPartEntity {

public UploadImagePayloa

...

您的代码中存在错误,但不是您发布的代码。 检查您编码的位图的内容,并在解码时检查您是否使用了正确的输入字符串变量。 您的编码,解码部分工作正常。 但是,如果您不依赖成员字段,那么代码会更好。 public String getStringImage(Bitmap bmp)

{

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bmp.compress(Bitmap.CompressFormat.JPEG, 100,

...

最后,我找到了解决我的问题的方法。 这可能有助于其他如何面对同样的问题。 1)更改了@Srikanth引用的C#编码 HttpServerUtility.UrlTokenEncode to System.Convert.ToBase64String(bytes);

2)我将base64编码保存到C#文件中,并从NodeJS中读取。 问题是保存到文件添加额外的字符,使解码使用NodeJS(总是得到损坏的zip)UTF8问题的问题。 用于将zip文件转换为Base64编码的C#代码 using S

...

不幸的是,Base64不是base64。 实现中可能存在差异。 一些实现例如插入行在编码时每76个字符中断,有些则不然。 Base64 is not base64 unfortunately. There may be differences in the implementations. Some implementation for example insert line breaks every 76 characters when encoding, some don't.

您可以使用它来解码Base64。 将它保存到文件更加棘手 ,您需要知道将它放在何处。 You can use this to decode Base64. Saving it to file is more tricky, you need to know where to put it.

您的所有问题的答案都在文档中。 首先,我们来看看AudioSystem的文档 。 有五个getAudioInputStream方法。 两个采用显式的AudioFormat参数,这些参数不适用于播放.wav文件。 其余三种方法分别采用File,InputStream和URL。 由于您已经有一个字节数组,最好的选择是将字节包装在ByteArrayInputStream中 。 现在我们有一个InputStream,我们可以传递给getAudioInputStream方法。 如果您想知道如何获取Clip对

...

 类似资料: