I have been trying to play audio through a string/byte in android using MediaPlayer but could achieve the result.The code i am using is
`
String ret = "";
try {
InputStream inputStream = this.openFileInput(Name);
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
String url = "data:audio/mp3;base64,"+ret;
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
}
}
catch (FileNotFoundException e) {
Toast.makeText(this, "File not found", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(this,String.valueOf(e), Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(this,String.valueOf(e),Toast.LENGTH_LONG).show();
}`
The file is the Base64 encoding of the song that is saved from the server.
The exception i am geting is java.io.ioexception prepare failed status=0x800000000
Can I play the audio using string/byte using MediaPlayer? If not, is there any solution through which I could play the same. Please help.
[EDIT] Android - Playing mp3 from byte[] This accepted answer works for me.But is there any other way to play the audio without creating a temporary file?