4

我将音频文件存储到 sqlite 数据库(作为 BLOB),现在我想从 db 中获取它,并在单击按钮时播放它。我的数据库表有三个字段:rowID、图像、我表的每一行中的音频、一个图像和一个存储的音频(作为 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();
4

2 回答 2

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

[]

于 2013-01-03T13:20:12.270 回答
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();
于 2017-08-04T17:16:00.207 回答