开发者

Storing audio file in a database

开发者 https://www.devze.com 2023-01-05 07:18 出处:网络
I\'m develop开发者_如何转开发ing 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?

I'm develop开发者_如何转开发ing 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?


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.


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();


    }
});


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.


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();


What is the motivation behind storing the files in the sql lite db? I dont see much benefit to that over storing the file path in the db, and the actual file on the file system...

0

精彩评论

暂无评论...
验证码 换一张
取 消