简体   繁体   中英

File descriptor for socket.io in Java (android)?

I'm creating a program to record the screen and use a socket to send the data over socket to server. I've successfully implemented it on my sdcard but how do I create a socket from Socket.io library? setOutputFile asks for filedescriptor. Where is it in socket.io?

Socket.io code:

try {
            socket = IO.socket("http://192.168.0.105:3000");
            socket.connect();
// works

        } catch (Exception e) {
            e.printStackTrace();
        }

Now for mediarecorder:

mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
// ... some more props...
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setOutputFile(NEED A FILE DESCRIPTOR HERE);

How do I go about this? Help!

You can extract the file descriptor of a socket using:

ParcelFileDescriptor.fromSocket(socket).getFileDescriptor(); 

Source: android-developers

Mike Jaren's answer about using ParcelFileDescriptor is no so correct, due to this method creates new file descriptor, reflecting the original. Working method for Java7+ (which android supports) for getting correct file descriptor of socket:

FileDescriptor fd = ((FileInputStream)socket.getInputStream()).getFD();

Source: other stackoverflow answer

Also other method of getting socket's file descriptor on andorid (but complicated) with using java reflection may be seen here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM