I know this is old, but I searched for an answer and didn't found any which really allows to play from a byte array without temp files, and indeed there is a very easy way, creating a custom MediaDataSource.
I'm posting here the code for Xamarin Android, but it's very easily ported to Java, the question is you can see the method to do it.
Note that these methods were added on the android 6.0 SDK, if you target lower versions of Android you must use the correct compatibility support library.
public class AudioPlayer
{
MediaPlayer currentPlayer;
public void Play(byte[] AudioFile, bool Loop)
{
Stop();
currentPlayer = new MediaPlayer();
currentPlayer.Prepared += (sender, e) =>
{
currentPlayer.Start();
};
currentPlayer.Looping = Loop;
currentPlayer.SetDataSource(new StreamMediaDataSource(new System.IO.MemoryStream(AudioFile)));
currentPlayer.Prepare();
}
public void Stop()
{
if (currentPlayer == null)
return;
currentPlayer.Stop();
currentPlayer.Dispose();
currentPlayer = null;
}
}
public class StreamMediaDataSource : MediaDataSource
{
System.IO.Stream data;
public StreamMediaDataSource(System.IO.Stream Data)
{
data = Data;
}
public override long Size
{
get
{
return data.Length;
}
}
public override int ReadAt(long position, byte[] buffer, int offset, int size)
{
data.Seek(position, System.IO.SeekOrigin.Begin);
return data.Read(buffer, offset, size);
}
public override void Close()
{
if (data != null)
{
data.Dispose();
data = null;
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (data != null)
{
data.Dispose();
data = null;
}
}
}