【问题标题】:playing Audio from BLOB field of a Table of a SQLite DB从 SQLite DB 表的 BLOB 字段播放音频
【发布时间】:2012-07-18 06:58:29
【问题描述】:

我将音频文件存储到 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();

【问题讨论】:

    标签: android database audio blob playback


    【解决方案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);
    }
    

    []

    【讨论】:

      【解决方案2】:

      也许为时已晚,但如果有人需要的话。

      这对我有用:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-05
        • 2022-01-13
        • 1970-01-01
        • 2011-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多