我正在尝试制作一个用MediaRecorder
录制音频的示例,其中记录的数据通过Android上的UDP套接字传递(> = 4.0)。
阅读文章: Broadcasting video with Android - without writing to local files, 我使用了ParcelFileDescriptor.fromDatagramSocket
这是我目前正在研究的简化伪代码:
//private DatagramSocket ds;
//private MediaRecorder mRecorder;
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(ds);
mRecorder = new MediaRecorder();
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setOutputFile(pfd.getFileDescriptor());
mRecorder.prepare();
mRecorder.start();
这里只是一个简单的DatagramSocket(UDP)侦听器线程循环来观察DatagramSocket ds
//...
private DatagramSocket ds;
private int localPort = 39000;
private MediaRecorder mRecorder;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
ds = new DatagramSocket(localPort);
}
catch (SocketException e)
{
System.out.println(e);
}
class IOThread extends Thread
{
@Override
public void run()
{
byte []buf = new byte[1024];
DatagramPacket packet= new DatagramPacket(buf, buf.length);
while (true)
{
try
{
ds.receive(packet);
SocketAddress sockAddress = packet.getSocketAddress();
int len = packet.getLength();
String msg = new String(buf, 0, len);
System.out.println(msg + ":" + len + "byte receive by "+ sockAddress.toString());
}
catch (IOException e)
{
System.out.println(e);
}
}
}
}
IOThread io1 = new IOThread();
io1.start();
//..........
原则上,我希望MediaRecorder
记录的缓冲区通过UDP FileDescriptor传递,不知何故我可以管理套接字,但到目前为止,我没有发现任何事情。
我确认UDP侦听器单元与其他UDP输入一起正常工作,并且MediaRecorder
单元应该没问题,因为它适用于localFiles而不是UDP套接字FileDescriptor。
有什么建议吗?
提前致谢!
答案 0 :(得分:0)
可能最好通过MediaRecorder - > LocalSocket,
这是我随后的Q& A
a LocalSocket (Unix domain) client-server data flow issue for MediaRecorder in Android (Java)