I am trying to fallow Truiton ScreenCapture example, in order to record the device screen using MediaProjection
When saving the recording localy it works
You have to use LocalServerSocket, below is what partially worked for me, I have a MediaStreamer class which extends MediaRecorder.
public class MediaStreamer extends MediaRecorder {
private LocalServerSocket localServerSocket = null;
private LocalSocket receiver, sender = null;
public void prepare() throws IllegalStateException,IOException {
receiver = new LocalSocket();
try {
localServerSocket = new LocalServerSocket("<your_socket_addr>");
receiver.connect(new LocalSocketAddress("<your_socket_addr>"));
receiver.setReceiveBufferSize(4096);
receiver.setSendBufferSize(4096);
sender = localServerSocket.accept();
sender.setReceiveBufferSize(4096);
sender.setSendBufferSize(4096);
} catch (IOException e1) {
throw new IOException("Can't create local socket !");
}
setOutputFile(sender.getFileDescriptor());
try {
super.prepare();
} catch (Exception e) {
closeSockets();
throw e;
}
}
public InputStream getInputStream() {
InputStream out = null;
try {
out = receiver.getInputStream();
} catch (IOException e) {
}
return out;
}
public void stop() {
closeSockets();
super.stop();
}
private void closeSockets() {
if (localServerSocket !=null) {
try {
localServerSocket.close();
sender.close();
receiver.close();
}
catch (IOException e) {
}
localServerSocket = null;
sender = null;
receiver = null;
}
}
}
For Recording
video = new MediaStreamer();
video.reset();
video.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
video.setPreviewDisplay(holder.getSurface());
video.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
video.setVideoFrameRate(VideoConstants.frameRate);
video.setVideoEncodingBitRate(VideoConstants.bitRate*1000);
video.setVideoSize(VideoConstants.resolationX, VideoConstants.resolationY);
video.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
try {
video.prepare();
} catch (IOException e) {
e.printStackTrace();
}
video.start();
There is no easy way to perform post-processing on the data to manually add the file headers. So the solution is either don't use MP4 as the recording format, or, write a packetiser similar to what is used in the Spydroid project
Hope this helps !