MediaRecorder.stop()失败:-1007重新启动后

时间:2019-03-14 09:17:57

标签: java android mediarecorder

我录制Android手机屏幕上的视频,并在录制后每5秒发送一次结果文件,由于使用UDP,它分成了65K字节长的块。这是代码:

@Override
    public void run() {
        FileInputStream fis = null;
        try
        {
            fis = new FileInputStream(sendFile);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        if (screenRecorder.isRunning()) screenRecorder.stopRecord();
        try {
            byte[] videoBytes = new byte[65000];
            if (sendFile.exists()) {
                int piecesNumber = (int)(sendFile.length() / 65000) + 1;
                byte[] byteNum = ByteBuffer.allocate(4).putInt(piecesNumber).array();
                sock.send(new DatagramPacket(byteNum, byteNum.length, ia, 11111));
                for (int i = 0; i < piecesNumber; i++) {
                    fis.read(videoBytes);
                    DatagramPacket videoPack = new DatagramPacket(videoBytes, 65000, ia, 11111);
                    sock.send(videoPack);
                }
                try {
                    PrintWriter flusher = new PrintWriter(sendFile); // flush file for new part of vid
                    flusher.write("");
                    flusher.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    fis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Log.d("RECORDED", "Sent " + videoBytes.length + " bytes");
            } else Log.e("FILE", "Not found");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!screenRecorder.isRunning()) screenRecorder.startRecord();
    }

screenRecorder.startRecord()和stopRecord()的代码为:

public boolean startRecord() {
    if (mediaProjection == null || running) {
        return false;
    }

    initRecorder();
    if (virtualDisplay == null) createVirtualDisplay();
    mediaRecorder.start();
    running = true;
    return true;
}

public boolean stopRecord() {
    if (!running) {
        return false;
    }
    running = false;
    mediaRecorder.stop();
    //mediaRecorder.reset();
    //virtualDisplay.release();
    //mediaProjection.stop();

    return true;
}

reset()取消注释不会更改任何内容,对virtualDisplay.release()和mediaProjection.stop()进行注释是因为我在开始记录之前初始化了VirtualDisplay和MediaProjection的实例,并且不想对其进行初始化。

计时器的第一个迭代进行得很顺利,我在接收PC上获得了视频的第一个5秒。但是在下一个stop()调用中,当前活动关闭,这就是我在错误日志中看到的内容:

    2019-03-14 12:01:37.121 18374-18473/com.petrsu.se.s2s E/MediaRecorder: stop failed: -1007
2019-03-14 12:01:37.125 18374-18473/com.petrsu.se.s2s E/AndroidRuntime: FATAL EXCEPTION: Timer-0
    Process: com.petrsu.se.s2s, PID: 18374
    java.lang.RuntimeException: stop failed.
        at android.media.MediaRecorder.native_stop(Native Method)
        at android.media.MediaRecorder.stop(MediaRecorder.java:1415)
        at com.petrsu.se.s2s.ScreenRecorder.stopRecord(ScreenRecorder.java:80)
        at com.petrsu.se.s2s.DataTransfer$sendTask.run(DataTransfer.java:221)
        at java.util.TimerThread.mainLoop(Timer.java:555)
        at java.util.TimerThread.run(Timer.java:505)

这种行为的原因可能是什么?感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

修正:我不应该评论

virtualDisplay.release;

我应该在每次停止时释放它,然后在startRecord()中再次创建:

public boolean startRecord() {
    if (mediaProjection == null || running) {
        return false;
    }

    initRecorder();
    createVirtualDisplay(); // no additional check
    mediaRecorder.start();
    running = true;
    return true;
}