Android: Video-streaming with Mediarecorder over Sockets using ParcelFileDescriptor

对着背影说爱祢 提交于 2020-01-14 02:43:13

问题


I'm struggling with this quiet long and I decided I need help. The basis idea is described here: Link

I want to stream the output from the MediaRecorder directly to my Server over Sockets while recording. (In this specific case I want to see if this is possible without using any streaming protocol just via a HTTP-POST.)

The error of my current approach is: E/MediaRecorder: start failed: -2147483648 - RuntimeException

Before I had an error pointing me to this topic mediarecorder-issue-on-lollipop

I use a AsyncTask class to call my recordData() function:

new sendAsync().execute("test");


private void recordData(Socket socket){
    try{
        OutputStream out = socket.getOutputStream();
        InputStream is = null;

        ParcelFileDescriptor[] parcelFileDescriptors = ParcelFileDescriptor.createPipe();
        parcelRead = new ParcelFileDescriptor(parcelFileDescriptors[0]);
        parcelWrite  = new ParcelFileDescriptor(parcelFileDescriptors[1]);
        try{
            is = new ParcelFileDescriptor.AutoCloseInputStream(parcelRead);
        } catch (Exception e){
            e.printStackTrace();
        }
        //pfd = ParcelFileDescriptor.fromSocket(socket);
        sft = new SurfaceTexture(0);
        sf = new Surface(sft);

        if(recorder == null) {
            recorder = new MediaRecorder();
            recorder.reset();
            recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
            recorder.setVideoFrameRate(30);
            recorder.setPreviewDisplay(sf);

            recorder.setOutputFile(parcelWrite.getFileDescriptor());

            recorder.setMaxDuration(10000); // 10 seconds
            try {
                recorder.prepare();
                recorder.start();
                System.out.println("This is Recorder running");
            } catch (IOException e) {
                e.printStackTrace();
            }

            byte[] buffer = new byte[16384];
            int byteread = 0;
            while ((byteread = is.read(buffer, 0, buffer.length)) != -1) {
                out.write(buffer, 0, byteread);
            }
            out.flush();
        }else{
            stopRecording();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Sorry for the bad coding-style.

I tried to change the OutputFormat and VideoEncoder following other solutions found to the topic. But nevertheless I'm still not sure if I'm going in the right direction.

Even after coming around this bug I think I somehow need to read out and send the Stream to the server in its own thread.

Every hint could help.


回答1:


I'm not sure if this is an error due to latest versions on Android (tested on Nougat and Oreo). But as much I tried to find a workaround I still ended up on the same error message with trying to use the MediaRecorder. Thanks to libstreaming I use now the MediaCodec API explained in their example how to use the Library: Example 2.

Then I take the Inputstream of the MediaCodec API and read it into a byte array until a certain size is reached and send this to my http-server.



来源:https://stackoverflow.com/questions/45998567/android-video-streaming-with-mediarecorder-over-sockets-using-parcelfiledescrip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!