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

发布于 2024-09-08 14:25:49 字数 191 浏览 10 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鱼忆七猫命九 2024-09-15 14:25:49

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

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.

难理解 2024-09-15 14:25:49

尝试以下代码...它为我存储......

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


    }
});

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


    }
});
梦里的微风 2024-09-15 14:25:49

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

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.

执手闯天涯 2024-09-15 14:25:49

将声音文件存储在数据库上的主要原因可能是产品线方法。只需修改数据库而不触及代码,您就可以开发许多类似的应用程序。

我不对应用程序的大小发表评论,因为有关于它的评论,但我不看它。

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

检查一下:

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

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();
一刻暧昧 2024-09-15 14:25:49

将文件存储在 sql lite 数据库中的动机是什么?我认为与将文件路径存储在数据库中以及文件系统上的实际文件相比,没有多大好处......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文