Marcin S.
Marcin S.

Reputation: 11191

How can I play an audio stream from a BLOB from an external server?

In my application I have sent an audio 3gp file to the server and then using PHP I have stored it in a MySQL database as a BLOB. Is there a way to use a MediaPlayer in android to play an audio stream that is read directly from a BLOB? I know that I could read a BLOB and save it in a filesystem and then play it in android without downloading it but I would like to do not save any files in the server if possible. Thanks for the advises.

Upvotes: 0

Views: 2194

Answers (2)

jimp
jimp

Reputation: 17487

Presuming that MediaPlayer will let you feed it a URL, you could stream it via a PHP script that fetches the BLOB and echoes it to the output all without ever saving it to the filesystem or Android device. If your PHP script supports HTTP Range responses, you could even support seeking.

The script smartReadFile.php is a PHP script for streaming files with respect to Range requests.
https://groups.google.com/forum/#!msg/jplayer/nSM2UmnSKKA/bC-l3k0pCPMJ

Upvotes: 1

G M Ramesh
G M Ramesh

Reputation: 3412

try the sample following code: try to change according to your requirement, this code only reads the file from 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();

Upvotes: 1

Related Questions