Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to record video on Android into Stream

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.

like image 798
4ntoine Avatar asked Jan 30 '13 06:01

4ntoine


2 Answers

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.

like image 101
Franci Penov Avatar answered Oct 14 '22 13:10

Franci Penov


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?

like image 7
4ntoine Avatar answered Oct 14 '22 12:10

4ntoine