Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing audio file in a database

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

One solution is putting all the audio files in the resources folder and referring from there. This will never be a feasible solution as the application size will increase. Is there any way to convert the audio file into some format and dump into the SQLite database and retrieve them flexibly? If so, what options do I have?

like image 894
Vinayak Bevinakatti Avatar asked Jul 02 '10 10:07

Vinayak Bevinakatti


4 Answers

Storing the files as BLOB in a SQLite db will not save you any space vs storing them as files. If these files are not meant to be associated to any dynamic data, I would just put them in assets folders, that way they'll be compiled into the APK and might be a tad faster to retrieve that if they were in a db.

like image 177
Ricardo Villamil Avatar answered Nov 20 '22 14:11

Ricardo Villamil


try the following code... it 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();


    }
});
like image 22
G M Ramesh Avatar answered Nov 20 '22 12:11

G M Ramesh


Anybody correct me if I'm wrong, but SQLite has an restriction of total row size < 1024kb. If all of your audio files are small enough, you could store them as BLOBs.

like image 1
ognian Avatar answered Nov 20 '22 12:11

ognian


The main reason for storing sound files on database might be product line approach. You can develop many many similar applications by just modifying your database and not touching your code.

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

Yes. You can store your small sized sound files in sqlite database as blob fields. It works well. First store your data in database then retrieve it and put it in a temp file. That way you can store your sound files in 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();
like image 1
Or Uc Avatar answered Nov 20 '22 13:11

Or Uc