2012-07-19 22 views
4

我將我的音頻文件存儲到sqlite數據庫(如BLOB),現在我想從db中獲取它,並在單擊按鈕時播放它。 我的分表有三個字段:rowID,圖像,音頻 ,在我的表的每一行中,圖像和音頻存儲(如BLOB)。從SQLite DB的BLOB字段播放音頻

我嘗試這一點,但我沒有工作:

byte[] byteAudio2 = null; 

Cursor cur1 = db.query("audiofromdb_tbl", null, null, null, null, null, null); 

cur1.moveToFirst(); 
byteAudio2 = cur1.getBlob(cur1.getColumnIndex("audio"));   
File tempWav = null; 
    FileOutputStream fos = new FileOutputStream(tempWav); 
    fos.write(byteAudio2); 
fos.close(); 

MediaPlayer mp = new MediaPlayer(); 
mp.setDataSource(fos.getFD()); 
mp.prepare(); 
mp.start(); 

回答

1

嘗試這樣的事:

byte[] buffer = new byte[2048]; 
int size = 0; 
ByteArrayInputStream byteArrayStream = new ByteArrayInputStream(byteAudio2); 

while ((size = byteArrayStream.read(buffer, 0, buffer.length)) > 0) { 
    fos.write(buffer, 0, size); 
} 

[]

1

也許爲時已晚,但如果有人需要它。

這個工作對我來說:

File file; 
FileOutputStream fos; 

byteaudio = cursor.getBlob(cursor.getColumnIndex("sonido")); 

try { 

     file = File.createTempFile("sound", "sound"); 
     fos = new FileOutputStream(file); 
     fos.write(byteaudio); 
     fos.close(); 

     Log.d("File", file.toString()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
mp = MediaPlayer.create(getActivity(), Uri.fromFile(file)); 
      mp.start();