How to record video on Android into Stream

4ntoine picture 4ntoine · Jan 30, 2013 · Viewed 9.6k times · Source

Android MediaRecorder allows to save video to file (file or socket):

setOutputFile(FileDescriptor fd);
setOutputFile(String path)

How to save videodata to OutputStream? It will be used for streaming video recording.

Answer

Franci Penov picture Franci Penov · Jan 30, 2013

You can do it using ParcelFileDescriptor.fromSocket():

String hostname = "example.com";
int port = 1234;

Socket socket = new Socket(InetAddress.getByName(hostname), port);

ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);

MediaRecorder recorder = new MediaRecorder();
recorder.setOutputFile(pfd.getFileDescriptor());
recorder.prepare();
recorder.start();

If you prefer UDP, use ParcelFileDescriptor.fromDatagramSocket() instead.

Credit where credit is due.