Android MediaRecorder 和 setOutputFile

2024-04-22

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


当然,您可以稍后读取该文件并以处理的方式对它执行任何您想要的操作。假设您保存生成的音频文件的 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);
   }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android MediaRecorder 和 setOutputFile 的相关文章

随机推荐