Streaming audio from Android to desktop application
What the title says really. I need to stream the audio from the microphone on the telephone and play it in a desktop application (also Java code) on a computer.
Using UDP or TCP does not matter for me, whatever works best. Phone and computer will be on same NAT anyway so transmission will work fine.
I have a fair idea of how to send the stream data from the dev开发者_运维问答ice using this code:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
String host = "10.0.2.2";
int port = 5740;
Socket socket = null;
try {
socket = new Socket(InetAddress.getByName(host), port);
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
recorder.setOutputFile(pfd.getFileDescriptor());
recorder.prepare();
recorder.start();
Log.d(TAG, "Sending audio for 20 seconds..");
Thread.sleep(20000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The problem is, how do I play this stream in my java application on the PC? Or is there a better way to stream the sound?
I have mined the internet for information on this but without any good results but surely somebody must have accomplished this before?
Thanks in advance for any kind of help!
I have done something very close to what you have been trying to do. I am using gstreamer on my server listening to a udp port. There is also a relay server written in java which is basically nothing more than a loopback socket. There is one server port which waits for a mobile client connections, upon receiving data, it dumps them all using DatagramPackets (java class for udp packets) into gstreamer's udp port. The only catch is to find the proper decoder for your gstreamer pipeline.
Can you write a client that just connects to your phone on that port and receives data?
Now, there are no mp4 java decoders, so you'll need to use another format. Take look at some sample apps using JavaLayer or JOgg. They both work with any InputStream, so as long as you can open a socket, you can play back your stream.
Also, I'm not sure about Android, but don't you need to open a ServerSocket and wait for connections?
精彩评论