0

在我的应用程序中,我向服务器发送了一个音频 3gp 文件,然后使用 PHP 将它作为 BLOB 存储在 MySQL 数据库中。有没有办法在 android 中使用 MediaPlayer 播放直接从 BLOB 读取的音频流?我知道我可以读取 BLOB 并将其保存在文件系统中,然后在不下载的情况下在 android 中播放它,但如果可能的话,我不想在服务器中保存任何文件。感谢您的建议。

4

2 回答 2

1

尝试以下示例代码:尝试根据您的要求进行更改,此代码仅从 BLOB 读取文件:

                   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();
于 2012-10-03T04:56:05.090 回答
1

假设 MediaPlayer 将允许您向其提供 URL,您可以通过 PHP 脚本对其进行流式传输,该脚本获取 BLOB 并将其回显到输出,而无需将其保存到文件系统或 Android 设备。如果你的 PHP 脚本支持 HTTPRange响应,你甚至可以支持搜索。

脚本smartReadFile.php是一个 PHP 脚本,用于根据Range请求流式传输文件。
https://groups.google.com/forum/#!msg/jplayer/nSM2UmnSKKA/bC-l3k0pCPMJ

于 2012-10-03T04:56:18.523 回答