13

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.

4ntoine
  • 17,607
  • 16
  • 70
  • 175

2 Answers2

14

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.

Franci Penov
  • 71,783
  • 15
  • 124
  • 160
  • 1
    I knew it makes sense they would have included a way to specify something other than a file to receive the bytes, in setoutpuFile() – WIllJBD Jan 30 '13 at 08:20
  • thsi results into java.net.ConnectException: failed to connect to /127.0.0.1 (port 8090): connect failed: ECONNREFUSED (Connection refused) – 4ntoine Jan 31 '13 at 12:02
  • Why are you trying to connect to localhost? Do you actually have something running on the phone/emulator that listens on that port? – Franci Penov Jan 31 '13 at 19:05
  • 2
    I have setup a tcp server, whenever I do this I dont receive anything in the tcp server. I have checked the socket it sends the data fine and giving path also works fine. When I see the logs I see "/MediaRecorder﹕ start failed: -38" – Kamran May 14 '15 at 12:27
7

Using Android-specific LocalServerSocket seems to be the only possible way to get video data as stream. In brief, you have to:

  1. create LocalServerSocket instance
  2. set it as output file to MediaRecorder instance using file descriptor (mediaRecorder.setOutputFile(FileDescriptor fd);)
  3. accept connection
  4. read bytes from it (as from InputStream) in separate thread in loop

Another ideas?

Community
  • 1
  • 1
4ntoine
  • 17,607
  • 16
  • 70
  • 175