MediaRecorder and UDP (DatagramSocket) in Android
I am currently writing a program that is a sort of VoIP client. I am trying to establish a connection between 2 clients and send and receive data, that is recorded from microphone.
I am currently using files but it looks like an awful implementation:
The recorder starts recording to a temporary file. A timer kicks in, stop the recorder, look at the file size, to then send the content using a DatagramPacket.
It seems awfully bad. I wonder if the MediaRecorder and MediaPlayer class have a way to take a UPD socket as it's file output and input respectively (that is, a DatagramSocket).
I can 开发者_StackOverflow中文版use a file descriptor from a socket, but sockets uses TCP. I wanted to use UDP, so I guess DatagramSocket, but I can't seem to find a way to use it in my MediaRecorder and MediaPlayer class.
Does anyone please have a good solution for this?
Thank you very much
You are probably interested in a ParcelFileDescriptor, and then specifically the fromDatagramSocket(...) function, then you could use the getFileDescriptor function to set the Datasource of the player
*update: this only works offcourse if you have android 4.0 or higher: see the api limit
All,
One way you can do this is by implementing a second thread that sets up a LocalServerSocket in the abstract space (UNIX named socket). In the first thread you then create a LocalSocket that connects to the LocalServerSocket. After this step, you can obtain the FileDescriptor of the LocalSocket in the first thread, and set this as the OutputFile for MediaRecorder. In the second thread you just read from the peeled-off LocalSocket, and then write into your DatagramSocket. Ez Pz.
Good luck, B-Rad
精彩评论