简体   繁体   English

在Android上录制音频而不是文件

[英]Recording audio not to file on Android

I want android.media.MediaRecorder. 我想要android.media.MediaRecorder。 to record audio not into file, but into same variable, for example char[ ] or byte[ ] or some other datta buffer structure. 将音频不记录到文件中,而是记录到相同的变量中,例如char []或byte []或其他一些datta缓冲结构。 I want to send it to the remote server via Wi-Fi, can android.media.MediaRecorder provide this functionality? 我想通过Wi-Fi将它发送到远程服务器,android.media.MediaRecorder可以提供这个功能吗?

What you can do here is utilize the ParcelFileDescriptor class. 你可以在这里做的是使用ParcelFileDescriptor类。

//make a pipe containing a read and a write parcelfd
ParcelFileDescriptor[] fdPair = ParcelFileDescriptor.createPipe();

//get a handle to your read and write fd objects.
ParcelFileDescriptor readFD = fdPair[0];
ParcelFileDescriptor writeFD = fdPair[1];

//next set your mediaRecorder instance to output to the write side of this pipe.
mediaRecorder.setOutputFile(writeFD.getFileDescriptor());

//next create an input stream to read from the read side of the pipe.
FileInputStream reader = new FileInputStream(readFD.getFileDescriptor());

//now to fill up a buffer with data, we just do a simple read
byte[] buffer = new byte[4096];//or w/e buffer size you want

//fill up your buffer with data from the stream
reader.read(buffer);// may want to do this in a separate thread

and now you have a buffer full of audio data 现在你有一个充满音频数据的缓冲区

alternatively, you may want to write data directly to a socket from the recorder. 或者,您可能希望将数据直接从记录器写入套接字。 this can also be achieved with the ParcelFileDescriptor class. 这也可以通过ParcelFileDescriptor类实现。

//create a socket connection to another device
Socket socket = new Socket("123.123.123.123",65535);//or w/e socket address you are using

//wrap the socket with a parcel so you can get at its underlying File descriptor
ParcelFileDescriptor socketWrapper = ParcelFileDescriptor.fromSocket(socket);

//set your mediaRecorder instance to write to this file descriptor
mediaRecorder.setOutputFile(socketWrapper.getFileDescriptor());

now any time your media recorder has data to write it will automatically write it over the socket 现在,只要您的介质记录器有数据写入,它就会自动将其写入套接字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM