代码之家  ›  专栏  ›  技术社区  ›  Vinayak Bevinakatti

在数据库中存储音频文件

  •  6
  • Vinayak Bevinakatti  · 技术社区  · 16 年前

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

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

    5 回复  |  直到 9 年前
        1
  •  6
  •   Ricardo Villamil    16 年前

    将文件存储为sqlitedb中的BLOB与将它们存储为文件相比不会节省任何空间。如果这些文件不打算与任何动态数据相关联,我只会将它们放在assets文件夹中,这样它们就会被编译到APK中,如果它们在db中,检索它们可能会快一点。

        2
  •  4
  •   Suragch Shmidt    8 年前

    请尝试以下代码。。。它是为我储存的。。。。。

        @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();
    
    
        }
    });
    
        3
  •  1
  •   ognian    16 年前

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

        4
  •  1
  •   Or Uc    10 年前

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

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

    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();
    
        5
  •  0
  •   Ben    16 年前

    在sqllitedb中存储文件的动机是什么?我认为在数据库中存储文件路径和文件系统中的实际文件没有什么好处。。。

    推荐文章