Android的MediaRecorder和setOutputFile [英] Android MediaRecorder and setOutputFile

查看:474
本文介绍了Android的MediaRecorder和setOutputFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过的Andr​​oid SDK和我发现MediaRecorder类可以从一个摄像头,音频或其它来源和COM preSS它的输入。通过setOutputFile方法,你可以指定你想要的数据存储(文件或URI),但如果我要存储的数据在内存缓冲区,并通过连接发送?或者发送之前处理它?我的意思是有没有办法不创建一个文件,但使用的内存缓冲区只?

I've read the Android SDK and I've found that the MediaRecorder class can take input from a Camera, Audio or other source and compress it. Through the setOutputFile method you can specify where you want the data to be stored (File or URI), but what if I want to store that data in a memory buffer and send it over a connection? Or process it before sending it? I mean is there a way not to create a file but to use a memory buffer only?

推荐答案

当然,你可以在后面读取该文件,做任何你想做的事情的处理方式。假设,美持有的URI来生成的音频文件,这里要说的是读入一个字节数组code片断,然后删除该文件。

You can of course read the file in later and do whatever you want with it in the way of processing. Assuming that u holds the Uri to the resulting audio file, here is a code snippet that reads it into a byte array and then deletes the file.

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

这篇关于Android的MediaRecorder和setOutputFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆