1

我正在尝试使用 MediaRecorder 通过 UDP 流式传输视频,但是这段代码给我留下了这个错误:

E/MediaRecorder: prepare failed: -38

我正在使用 RXJava2 来确保DatagramSocket在另一个线程上创建,我的代码如下:

public class MainActivity extends AppCompatActivity {
    ...

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        Observable.just(0) // Yep, that's one ugly way to start...
                .observeOn(Schedulers.io())
                .map(i -> createFileDescriptor())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::createMediaRecorder);
        ...
    }

    private ParcelFileDescriptor createFileDescriptor() {
        try {
            String localHostName = "192.168.1.1";
            String remoteHostName = "192.168.1.2";
            int port = 5004;
            DatagramSocket ds = new DatagramSocket(port, InetAddress.getByName(localHostName));
            ds.connect(InetAddress.getByName(remoteHostName), port);
            return ParcelFileDescriptor.fromDatagramSocket(ds);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void createMediaRecorder(ParcelFileDescriptor parcelFileDescriptor) {
        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(1280, 720);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mMediaRecorder.setOutputFile(parcelFileDescriptor.getFileDescriptor());

        try {
            mMediaRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mMediaRecorder.start();
    }
}

如果我改为保存到文件,则代码可以工作,因此它必须与FileDescriptorDatagramSocket.

有什么建议么?

4

0 回答 0