开始运行时,我的按钮不再响应



我有以下按钮:

prStartBtn.setOnClickListener(new View.OnClickListener() {
        // @Override
        public void onClick(View v) {
            if (prRecordInProcess == false) {
                startRecording();
            } else {
                stopRecording();
            }
        }
    });

当我第一次按下它时,它会这样做:

private boolean startRecording() {
    prCamera.stopPreview();
    try {
        prCamera.unlock();
        prMediaRecorder.setCamera(prCamera);
        prMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        String lVideoFileFullPath;
        String lDisplayMsg = "Current container format: ";
        prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); prMediaRecorder.setVideoEncoder(VideoEncoder.H264);
        if (first) {
            lVideoFileFullPath = cVideoFilePath + "result" + lVideoFileFullPath;
        } else {
            lVideoFileFullPath = cVideoFilePath + "vid2" + lVideoFileFullPath;
        }
        final File f = new File(lVideoFileFullPath);
        f.createNewFile();
        prRecordedFile = new FileOutputStream(f);
        prMediaRecorder.setOutputFile(prRecordedFile.getFD());
    prMediaRecorder.setVideoSize(sizeList.get(Utils.puResolutionChoice).width, sizeList.get(Utils.puResolutionChoice).height);
        prMediaRecorder.setVideoEncodingBitRate(3000000);
        prMediaRecorder.setVideoFrameRate(cFrameRate);
        prMediaRecorder.setPreviewDisplay(prSurfaceHolder.getSurface());
        prMediaRecorder.setMaxDuration(cMaxRecordDurationInMs);
        prMediaRecorder.setMaxFileSize(cMaxFileSizeInBytes);
        prMediaRecorder.prepare();
        prMediaRecorder.start();
        final Runnable r = new Runnable() {
            public void run() {
                try {
                    fileIn = new FileInputStream(f);
                    while (prRecordInProcess) {
                        // prRecordedFile.flush();
                        System.out.println("bytesAvailable: " + fileIn.available());
                        Thread.sleep(1000);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // handler.postDelayed(this, 1000);
            }
        };
        handler.postDelayed(r, 1000);
        prStartBtn.setText("Pause");
        prRecordInProcess = true;
        return true;
    } catch (IOException _le) {
        _le.printStackTrace();
        return false;
    }
}

现在,如果我注释掉可运行程序,代码工作完美。如果我离开它,它运行(它告诉我文件是如何增长的),但我没有访问按钮了(如果我再次按下按钮,停止录制,它什么也不做),过了一会儿,它崩溃(ANR)。有什么办法解决这个问题吗?

你的Runnable在UI线程上启动;这就是UI被阻塞的原因,你会得到一个ANR。要在另一个线程中启动它,可以:

Thread thread = new Thread()
{
@Override
public void run() {
    try {
        fileIn = new FileInputStream(f);
        while (prRecordInProcess) {
             // prRecordedFile.flush();
             System.out.println("bytesAvailable: " + fileIn.available());
             Thread.sleep(1000);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
};
thread.start();

使用AsyncTask,它将在后台执行您的工作,并在完成后通知您的UI。

AsyncTasks应该理想地用于较短的操作(最多几秒钟)。如果需要让线程长时间运行,强烈建议使用java.util.concurrent包提供的各种api,如Executor、ThreadPoolExecutor和FutureTask。

最新更新