Stream MediaRecorder file while recording

I am trying to get a video stream from point A (2.1 Android phone) to point B (my server) in real time. How can i do this? Following are my attempts (a little long but short!)


The goal is to get hourly video recorded from the phone to the server without pausing / stopping the stream. a delay of up to several minutes is not a problem. I tried three approaches

  • a FileInputStream stream that reads destination video from a FileDescriptor
  • call MediaRecoder.setOutputFile on FD of the 'sender' connector. this connector connects to LocalSocketServer, the destination of which is the receiver socket.
  • open the socket for my server and providing setOutputFile your FD

unfortunately, both attempts failed.

  • Logs only 24 bytes when i call FileInputStream.available (), and the actual number of bytes after I call Recorder.stop ()
  • gives me this completely useless stacktrace

    ERROR / AndroidRuntime (18532): caused by: java.lang.RuntimeException: failed to start. ERROR / AndroidRuntime (18532): on android.media.MediaRecorder.start (native method) ERROR / AndroidRuntime (18532): in com.example.demovideo.DemoVideo.initializeCamera (...) ...

  • same error 2

code fragments (parts skipped)

one)

fileOut = new FileOutputStream(pathToFile); ... recorder.setOutputFile(fileOut.getFD()); recorder.prepare() recorder.start() // in an Async Thread fileIn = FileInputStream(fileOut.getFD); while (recording) { fos.flush(); Log.w("---", "bytesAvailable: " + fileIn.available()); //always returns 24 Thread.sleep(1000); } 

2)

 // in a Thread server = new LocalServerSocket(SOCKET_ADDRESS); while (true){ receiver = server.accept(); if (receiver != null){ InputStream input = receiver.getInputStream(); ... // processing would go here } } sender = new LocalSocket(); sender.connect(new LocalSocketAddress(SOCKET_ADDRESS)); recorder.setOutputFile(sender.getFileDescriptor()); ... recorder.prepare(); recorder.start(); // <- error 
  • correctly saves intact video to sd
  • works if i use setOutputFile (pathToFile). sockets also work when I run sender.getOutputStream().write(message.getBytes());
+6
java android video-streaming streaming mediarecorder
source share
1 answer
  • I created a mobile video streaming application with this approach and it worked. Therefore, this should be the right approach. Later, when I was no longer part of the project, I received reports that this approach did not work with some newer phones - primarily the Samsung Galaxy S. The problem was that these phones rarely dispelled video data, perhaps once a minute. Which phone do you use to test this?

  • & 3. MediaRecorder is a wrapper around its own library. I assume that this library wants a particular file to be written to a non-channel. At the file system level, files and channels look the same, but you cannot have random access to a channel (search).

+2
source share

All Articles