Android 将音频文件存储在数据库中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3165061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:59:26  来源:igfitidea点击:

Storing audio file in a database

androidaudioandroid-sqlite

提问by Vinayak Bevinakatti

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?

我正在开发一个应用程序。我需要使用至少 400 个音频文件,这些文件可以为一些相应的文本播放。我的问题是这是最好的和优化的方法来做到这一点?

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?

一种解决方案是将所有音频文件放在资源文件夹中并从那里引用。随着应用程序大小的增加,这将永远不是一个可行的解决方案。有什么办法可以将音频文件转换成某种格式转储到SQLite数据库中,灵活检索?如果是这样,我有哪些选择?

采纳答案by Ricardo Villamil

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.

与将文件存储为文件相比,将文件作为 BLOB 存储在 SQLite 数据库中不会为您节省任何空间。如果这些文件不打算与任何动态数据相关联,我会将它们放在资产文件夹中,这样它们就会被编译到 APK 中,如果它们在数据库中,检索速度可能会快一些。

回答by G M Ramesh

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


    }
});

回答by Or Uc

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.

是的。您可以将小型声音文件作为 blob 字段存储在 sqlite 数据库中。它运作良好。首先将数据存储在数据库中,然后检索它并将其放入临时文件中。这样您就可以将声音文件存储在数据库中。

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

回答by ognian

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.

如果我错了,任何人都可以纠正我,但 SQLite 有总行大小 < 1024kb 的限制。如果您的所有音频文件都足够小,则可以将它们存储为 BLOB。

回答by Ben

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...

将文件存储在 sql lite db 背后的动机是什么?我没有看到将文件路径存储在数据库中以及文件系统上的实际文件有多大好处......