我想要android.media.MediaRecorder。将音频记录到文件中而不是文件中,而是记录到相同的变量中,例如char []或byte []或其他一些datta缓冲区结构。我想通过Wi-Fi将其发送到远程服务器,android.media.MediaRecorder可以提供此功能吗?

最佳答案

您可以在这里利用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

现在您有一个充满音频数据的缓冲区

或者,您可能希望将数据直接从刻录机写入套接字。这也可以通过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());

现在,只要您的媒体刻录机有要写入的数据,它就会自动通过套接字写入数据

关于java - 录制音频不在Android上录制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14437571/

10-11 22:48
查看更多