Saving an audio file in a database

I am developing an application. I need to use at least 400 audio files that can be played for some relevant texts. My question is the best and optimized way to do this?

One solution puts all the audio files in a resource folder and links from there. This will not be a possible solution, as the size of the application will increase. Is there a way to convert the audio file to some format and dump it into a SQLite database and extract them flexibly? If so, what are my options?

+6
android audio android-sqlite
source share
5 answers

Saving files as a BLOB in SQLite db will not save you any space or save them as files. If these files are not intended to be associated with any dynamic data, I would just put them in the resource folders, so they will be compiled into the APK and can be a little faster to get it if they were in db.

+4
source share

try the following code ... it is stored for me .....

@Override public void onClick(View arg0) { SQLiteDatabase myDb; String MySQL; byte[] byteImage1 = null; byte[] byteImage2 = null; MySQL="create table emp1(_id INTEGER primary key autoincrement, sample TEXT not null, picture BLOB);"; myDb = openOrCreateDatabase("Blob List", Context.MODE_PRIVATE, null); myDb.execSQL(MySQL); String s=myDb.getPath(); textView.append("\r\n" + s+"\r\n"); myDb.execSQL("delete from emp1"); ContentValues newValues = new ContentValues(); newValues.put("sample", "HI Hello"); try { FileInputStream instream = new FileInputStream("/sdcard/AudioRecorder/AudioRecorder.wav"); BufferedInputStream bif = new BufferedInputStream(instream); byteImage1 = new byte[bif.available()]; bif.read(byteImage1); textView.append("\r\n" + byteImage1.length+"\r\n"); newValues.put("picture", byteImage1); long ret = myDb.insert("emp1", null, newValues); if(ret<0) textView.append("\r\n!!! Error add blob filed!!!\r\n"); } catch (IOException e) { textView.append("\r\n!!! Error: " + e+"!!!\r\n"); } Cursor cur = myDb.query("emp1",null, null, null, null, null, null); cur.moveToFirst(); while (cur.isAfterLast() == false) { textView.append("\r\n" + cur.getString(1)+"\r\n"); cur.moveToNext(); } ///////Read data from blob field//////////////////// cur.moveToFirst(); byteImage2=cur.getBlob(cur.getColumnIndex("picture")); bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0, byteImage2.length)); textView.append("\r\n" + byteImage2.length+"\r\n"); cur.close(); myDb.close(); } }); 
+4
source share

Someone will correct me if I am wrong, but SQLite has a limit on the total row size <1024KB. If all your audio files are small enough, you can save them as a BLOB.

+1
source share

The main reason for storing audio files in a database may be the approach to the product line. You can create many similar applications by simply modifying your database and not touching your code.

I do not comment on the size of the application, because there are comments about it, and I do not look at it.

Yes. You can store small audio files in the sqlite database as blob fields. It works well. First save your data in a database, then extract it and put it in a temporary file. This way you can store audio files in a database.

Check this:

 soundDataFile = File.createTempFile( "sound", "sound" ); FileOutputStream fos = new FileOutputStream( soundDataFile ); fos.write( soundData ); fos.close(); mediaPlayer.reset(); mediaPlayer.setDataSource( soundDataFile.getAbsolutePath() ); mediaPlayer.prepare(); mediaPlayer.start(); 
+1
source share

What is the motivation for storing files in sql lite db? I do not see much benefit from this, storing the file path in db, and the actual file in the file system ...

0
source share

All Articles