Android:使用 ParcelFileDescriptor 通过套接字使用 Mediarecorder 进行视频流传输

标签 android sockets exception streaming android-mediarecorder

我正在为这段安静的漫长岁月而苦苦挣扎,我决定需要帮助。 此处描述了基本想法:Link

我想在录制时通过套接字将 MediaRecorder 的输出直接流式传输到我的服务器。 (在这种特定情况下,我想看看是否可以不通过 HTTP-POST 使用任何流协议(protocol)。)

我目前的做法报错是:E/MediaRecorder: start failed: -2147483648 - RuntimeException

之前我有一个错误指向这个主题 mediarecorder-issue-on-lollipop

我使用 AsyncTask 类来调用我的 recordData() 函数:

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();
    }
}

抱歉糟糕的编码风格。

我尝试按照针对该主题找到的其他解决方案来更改 OutputFormat 和 VideoEncoder。但尽管如此,我仍然不确定我的方向是否正确。

即使解决了这个错误,我仍然认为我需要以某种方式读出 Stream 并将其发送到服务器自己的线程中。

每个提示都有帮助。

最佳答案

我不确定这是否是由于 Android 上的最新版本(在 Nougat 和 Oreo 上测试)导致的错误。但是尽管我试图找到解决方法,但我仍然在尝试使用 MediaRecorder 时遇到相同的错误消息。 感谢 libstreaming 我现在使用 MediaCodec API在他们的示例中解释了如何使用库:Example 2 .

然后我获取 MediaCodec API 的输入流并将其读入字节数组直到达到特定大小并将其发送到我的 http 服务器。

关于Android:使用 ParcelFileDescriptor 通过套接字使用 Mediarecorder 进行视频流传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45998567/

相关文章:

java - 在 Android 中隐藏 keystore 密码的最佳方法是什么?

java - 如何修复 org.apache.commons.compress.archivers.zip.UnsupportedZipFeatureException(epub mimetype)?

debugging - 如何在 x64 堆栈中找到 native 异常?

android - webview 没有在 android 的 fragment 内加载 url

android - Dojo/jQuery Mobile/等如何融入移动应用程序

ruby - 使用 ruby​​ 的聊天应用程序

c++ - 使用 getline() 而不设置 failbit

android - Xposed - 无法 Hook 静态方法

java - Android 媒体播放器在准备语句时出错

sockets - Firefox附加组件可以在UNIX套接字上监听吗?