Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - play mp3 from byte[] array

I have byte[] array, which contains mp3 file. Is there any way how to play mp3 file without creating temporary file?

Thanks

Edited:

I have tried temporary file approach:

                File tempMp3 = File.createTempFile("test", ".mp3", getCacheDir());

                //tempMp3.deleteOnExit();
                FileOutputStream fos = new FileOutputStream(tempMp3);
                fos.write(bytes);
                fos.close();

                mediaPlayer = MediaPlayer.create(getApplicationContext(),Uri.fromFile(tempMp3));
                mediaPlayer.start();

And I am still getting the NullPointerException. I am writing byte array (called bytes) to file, I am keeping its referrence and using it. But still getting NPE. Do you know where is the mistake?

like image 803
Waypoint Avatar asked Oct 09 '22 05:10

Waypoint


1 Answers

I am assuming you are using MediaPlayer and until the latest API there is no way to set the data in byte[] format just as a String(path), FileDescriptor, or Uri so I am afraid that you will have to write a temporary File.

like image 114
Ovidiu Latcu Avatar answered Oct 13 '22 09:10

Ovidiu Latcu