Play audio from a BLOB field in an SQLite DB table

I store my audio file in a sqlite database (like a BLOB), and now I want to get it from db, and also play it when I click the button. my db table has three fields: rowID, image, audio so that in each row of my table the image and sound are saved (like a BLOB).

I tried this but did not work for me:

byte[] byteAudio2 = null; Cursor cur1 = db.query("audiofromdb_tbl", null, null, null, null, null, null); cur1.moveToFirst(); byteAudio2 = cur1.getBlob(cur1.getColumnIndex("audio")); File tempWav = null; FileOutputStream fos = new FileOutputStream(tempWav); fos.write(byteAudio2); fos.close(); MediaPlayer mp = new MediaPlayer(); mp.setDataSource(fos.getFD()); mp.prepare(); mp.start(); 
+4
source share
2 answers

try something like this:

 byte[] buffer = new byte[2048]; int size = 0; ByteArrayInputStream byteArrayStream = new ByteArrayInputStream(byteAudio2); while ((size = byteArrayStream.read(buffer, 0, buffer.length)) > 0) { fos.write(buffer, 0, size); } 

[]

+1
source

Perhaps too late, but if someone needs it.

This works for me:

  File file; FileOutputStream fos; byteaudio = cursor.getBlob(cursor.getColumnIndex("sonido")); try { file = File.createTempFile("sound", "sound"); fos = new FileOutputStream(file); fos.write(byteaudio); fos.close(); Log.d("File", file.toString()); } catch (IOException e) { e.printStackTrace(); } mp = MediaPlayer.create(getActivity(), Uri.fromFile(file)); mp.start(); 
+1
source

All Articles