gpt4 book ai didi

java - 录制音频不要在 Android 上存档

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:55:30 25 4
gpt4 key购买 nike

我想要 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com