context 为语音文件的base64 编码内容
语音内容保存的地址为同一个地址 防止多个语音文件产生过多的缓存文件,占用内存空间
private void playerMedia(String context) {
try {
byte[] mp3SoundByteArray = Base64.decode(context, Base64.DEFAULT);// 将字符串转换为byte数组
// create temp file that will hold byte array
File tempMp3 = new File(getFilesDir().getAbsolutePath(), "yinbo.mp3");
tempMp3.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempMp3);
fos.write(mp3SoundByteArray);
fos.close();
// Tried reusing instance of media player
// but that resulted in system crashes...
MediaPlayer mediaPlayer = new MediaPlayer();
// Tried passing path directly, but kept getting
// "Prepare failed.: status=0x1"
// so using file descriptor instead
FileInputStream fis = new FileInputStream(tempMp3);
mediaPlayer.setDataSource(fis.getFD());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException ex) {
String s = ex.toString();
ex.printStackTrace();
}
}