Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android上录制音频而不是文件_Java_Android - Fatal编程技术网

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

Java 在Android上录制音频而不是文件,java,android,Java,Android,我想要android.media.MediaRecorder。不将音频录制到文件中,而是录制到同一个变量中,例如char[]或byte[]或其他DATA缓冲区结构。我想通过Wi-Fi将其发送到远程服务器,android.media.MediaRecorder能否提供此功能?您可以在这里使用ParcelFileDescriptor类 //make a pipe containing a read and a write parcelfd ParcelFileDescriptor[] fdPair

我想要android.media.MediaRecorder。不将音频录制到文件中,而是录制到同一个变量中,例如char[]或byte[]或其他DATA缓冲区结构。我想通过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
//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());
现在你有了一个充满音频数据的缓冲区

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

现在,只要您的媒体记录器有数据要写入,它就会自动通过套接字写入数据,这是可能的。MediaRecorder写入文件描述符,而不一定写入文件。请注意,这不是“流”!您必须再次写入服务器端的文件。这可能会有所帮助:实际上,您可以创建一个文件描述符,它只需写入内部缓冲区而不是文件,也不要忘记套接字也有一个文件描述符,您可以通过写入该文件描述符来写入套接字。只要有东西正在监听正确的套接字地址以接收这些数据包并相应地处理它们,就不需要编写文件服务器端。请注意(据我所知),除了使用输出格式AMR_NB,您不能在mediarecorder中使用不可查找的输出(如管道)