Android MediaRecorder 和 setOutputFile

7

我已经阅读了Android SDK,发现MediaRecorder类可以从相机、音频或其他源获取输入并进行压缩。通过setOutputFile方法,您可以指定要存储数据的位置(文件或URI),但如果我想将数据存储在内存缓冲区中并通过连接发送它呢?或者在发送之前处理它?我的意思是是否有一种方法可以不创建文件而只使用内存缓冲区?

1个回答

1

当然,您可以稍后读取文件并以任何方式进行处理。假设u保存了生成的音频文件的Uri,这里是一个代码片段,它将其读入字节数组,然后删除该文件。

String audioUri = u.getPath();
InputStream in = new BufferedInputStream(this.getContentResolver().openInputStream(u));
byte[] b = new byte[BUFSIZE];

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(mFileName/*mFilePath*/)));
int byteCnt = 0;
while (0 <= (byteCnt = in.read(b, 0, BUFSIZE)))
   out.write(b, 0, byteCnt);
out.flush();
out.close();
// try to delete media file
try {
   // Delete media file pointed to by Uri
   new File(getRealPathFromURI(u)).delete();
} catch (Exception ex) {}


   public String getRealPathFromURI(Uri contentUri) {
      String[] proj = { MediaStore.Images.Media.DATA };
      Cursor cursor = managedQuery(contentUri, proj, null, null, null);
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
      cursor.moveToFirst();
      return cursor.getString(column_index);
   }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接