rosu alin
rosu alin

Reputation: 5830

Starting a runnable, my buttons do not respond anymore

I have the following button:

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

When i first press it, it does this:

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

Now if i comment out the runnable, the code works perfect. If i leave it, it runs (it shows me how the file grows), but i do not have access to the buttons anymore (if I press the button again, to stop recording, it does nothing), and after a while, it crashes (ANR). Any ideas how to fix this?

Upvotes: 1

Views: 128

Answers (2)

Mohan Raj B
Mohan Raj B

Reputation: 1026

Use AsyncTask, that will executes your work in background and notifies your UI once it is finished.

AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask.

Upvotes: 1

etienne
etienne

Reputation: 3206

Your Runnable is launched on the UI thread; that's why the UI is blocked, and you get an ANR. To launch it in another thread, you can:

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

Upvotes: 2

Related Questions