How to record video on Android into Stream
Asked Answered
D

2

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.

Dionysiac answered 30/1, 2013 at 6:55 Comment(0)
D
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?

Dionysiac answered 30/1, 2013 at 7:56 Comment(3)
Hint: this is not possible with new Android versions (I guess starting at 5.x)Bernabernadene
@DenisLukenich why not?Ballenger
It's quite some time ago. I think it got blocked due to more strict security measures.Bernabernadene
M
15

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.

Meteoritics answered 30/1, 2013 at 8:14 Comment(4)
I knew it makes sense they would have included a way to specify something other than a file to receive the bytes, in setoutpuFile()Bowshot
thsi results into java.net.ConnectException: failed to connect to /127.0.0.1 (port 8090): connect failed: ECONNREFUSED (Connection refused)Dionysiac
Why are you trying to connect to localhost? Do you actually have something running on the phone/emulator that listens on that port?Meteoritics
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"Elver
D
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?

Dionysiac answered 30/1, 2013 at 7:56 Comment(3)
Hint: this is not possible with new Android versions (I guess starting at 5.x)Bernabernadene
@DenisLukenich why not?Ballenger
It's quite some time ago. I think it got blocked due to more strict security measures.Bernabernadene

© 2022 - 2024 — McMap. All rights reserved.