我创建了一个应用程序来使用本机 mediaPlayer 录制音频,将此音频文件转换为 base64 数据,并将其传递给 html5 音频标签,如下所示,
File file = new File(Environment.getExternalStorageDirectory()+"/"+ "audiofile"+"/"+"myAudio.mp3");
byte[] FileBytes =getBytesFromFile(file);
String base64 = Base64.encodeToString(FileBytes, Base64.NO_WRAP).toString();
public static byte[] getBytesFromFile(java.io.File file) throws IOException
{
InputStream is = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE)
{
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
is.close();
return bytes;
}
将此 base64 值传递给网页(此处为 base64 数据示例),
<div id="audiodiv" >
<audio id="audio_player" src="data:audio/mp3;base64,AAAAGGZ0eXAzZ3A0AAAAAGlzb20zZ3A0AAARTG1kYXQEYzzH8GMEOf/gAAAABDcYx/BtBDP/4AAAAAQ2PMfwZwQ1/+AAAAAE/xjH8G0EM//gAAAABDY8x/BnBDX/4AAAAAT/GMfwbQQz/+AAAAAENjzH8GcENf/gAAAABP8Yx/BtBDP/4AAAAAQ2PMfwZwQ1/+AAAAAE/xjH8G0EM//gAAAABDY8x/BnBDX/4AAAAAT/GMfwbQQz/+AAAAAENjzH8GcENf/gAAAABP8Yx/BtBDP/4AAAAAQ2PMfwZwQ1/+AAAAAE/xjH8G0EM//gAAAABDY8x/BnBDX/4AAAAAT/GMfwbQQz/+AAAAAENjzH8GcENf/gAAAABP8Yx/BtBDP/4AAAAAQ2PMfwZwQ1/+AAAAAE/xjH8G0EM//gAAAABDY8x/BnBDX/4AAAAAT/GMfwbQQz/+AAAAAENjzH8GcENf/gAAAABP8Yx/BtBDP/4AAAAAQ2PMfwZwQ1/+AAAAAE/xjH8G0EM//gAAAABDY8x/BnBDX/4AAAAAT/GMfwbQQz/+AAAAAENjzH8GcENf/gAAA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AAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAGldHJhawAAAFx0a2hkAAAAB1B/xQxQf8UMAAAAAQAAAAAAABqQAAAAAAAAAAAAAAAAAQAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAABQW1kaWEAAAAgbWRoZAAAAABQf8UMUH/FDAAAH0AAANSAAAAAAAAAACxoZGxyAAAAAAAAAABzb3VuAAAAAAAAAAAAAAAAU291bmRIYW5kbGUAAAAA7W1pbmYAAAAQc21oZAAAAAAAAAAAAAAAJGRpbmYAAAAcZHJlZgAAAAAAAAABAAAADHVybCAAAAABAAAAsXN0YmwAAABFc3RzZAAAAAAAAAABAAAANXNhbXIAAAAAAAAAAQAAAAAAAAAAAAEAEAAAAAAfQAAAAAAAEWRhbXIgICAAAIP/AAEAAAAgc3R0cwAAAAAAAAACAAAAAQAAAKAAAAFTAAAAoAAAABRzdHN6AAAAAAAAAA0AAAFUAAAAHHN0c2MAAAAAAAAAAQAAAAEAAAFUAAAAAQAAABRzdGNvAAAAAAAAAAEAAAAgAAAAGHVkdGEAAAAQU0RMTlNFUV9QTEFZ" controls="controls" autobuffer="autobuffer">
</audio>
</div>
但音频没有播放。你能告诉我我犯了什么错误吗?
注意:myAudio.mp3 是存储在 sdcard 中的录制音频文件