无限循环停止应用程序响应。该怎么办?

时间:2013-11-18 23:32:53

标签: java android multithreading

我写了一个小应用程序,在一个表面视图上绘制一个圆圈,该圆圈与到达手机麦克风的声音量成正比(声音越大,圆圈越大)。

一切都很好。我可以通过按下按钮开始操作,但随后我进入无限循环(按设计)。这会导致UI挂起。我想安排一些事情,以便我的应用程序运行并继续响应麦克风输入,但UI仍然是负责任的(即我可以按一个按钮(可能是后退按钮?)来停止应用程序。)。

目前应用程序直到android认为它在很长一段时间内没有响应(ANR?)并打开一个消息框以便我可以退出。

我该如何避免这种情况?我知道我应该使用一个新线程,但最简单的方法是什么?

    public class MainActivity extends ActionBarActivity {

        private static final int RECORDER_SAMPLERATE = 44100;
        private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
        private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
        private static final int BUFFSIZE = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
        private AudioRecord recorder = null;
        private SurfaceView mySurface;
        private static Handler handler;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mySurface = (SurfaceView) findViewById(R.id.surfaceView);
            recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, BUFFSIZE);
            recorder.startRecording();
        }

        private void respondToMIC() {

            Canvas myCanvas;
            Paint paint = new Paint();
            paint.setARGB(255, 255, 100, 100);

            short sData[] = new short[BUFFSIZE/2];
            while (true)
            {
                int read = recorder.read(sData, 0, BUFFSIZE/2);
                int min = 0;
                int max = 0;
                for (int j=0; j< read; j++)
                {
                    if (sData[j] < min) min = sData[j];
                    if (sData[j] > max) max = sData[j];
                }
                myCanvas = mySurface.getHolder().lockCanvas();
                myCanvas.drawColor(Color.WHITE);
                myCanvas.drawCircle(myCanvas.getWidth()/2, myCanvas.getHeight()/2, ((myCanvas.getHeight()/2)*Math.abs(max-min)/65000), paint);
                mySurface.getHolder().unlockCanvasAndPost(myCanvas);
            }
        }
    }

    <SurfaceView
        android:layout_width="fill_parent"
        android:layout_height="221dp"
        android:id="@+id/surfaceView"
        android:layout_gravity="center" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="surface"
        android:id="@+id/button3"
        android:onClick="respondToMIC"
        android:layout_gravity="center_horizontal|bottom" />

</FrameLayout>

2 个答案:

答案 0 :(得分:1)

您可以使用AsyncTask在后台处理录制器缓冲区,而不是阻止UI任务:

public class MainActivity extends ActionBarActivity {

    private static final int RECORDER_SAMPLERATE = 44100;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
    private static final int BUFFSIZE = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
    private AudioRecord recorder = null;
    private SurfaceView mySurface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mySurface = (SurfaceView) findViewById(R.id.surfaceView);
        recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, BUFFSIZE);
        recorder.startRecording();
    }

    private void respondToMIC() {
        new AudioRecordTask().execute();
    }

    private class AudioRecordTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            Canvas myCanvas;
            Paint paint = new Paint();
            paint.setARGB(255, 255, 100, 100);

            short sData[] = new short[BUFFSIZE / 2];

            // Run while the Activity is not finished.
            while (!isFinishing()) {
                int read = recorder.read(sData, 0, BUFFSIZE / 2);
                int min = 0;
                int max = 0;
                for (int j = 0; j < read; j++) {
                    if (sData[j] < min) min = sData[j];
                    if (sData[j] > max) max = sData[j];
                }
                myCanvas = mySurface.getHolder().lockCanvas();
                myCanvas.drawColor(Color.WHITE);
                myCanvas.drawCircle(myCanvas.getWidth() / 2, myCanvas.getHeight() / 2, ((myCanvas.getHeight() / 2) * Math.abs(max - min) / 65000), paint);
                mySurface.getHolder().unlockCanvasAndPost(myCanvas);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }
}

答案 1 :(得分:0)

这很可能包含错误,但请试一试:

       Thread micThread;

       private void respondToMIC() {

           if (micThread == null) {
               micThread = new Thread(new micRunnable());
               micThread.start();
           } else {
               // if you want the same button to shut it down
               stopRespondingToMic();
           }

        }

       private void stopRespondingToMic() {
           if (micThread != null && micThread.isAlive()) {
               micThread.interrupt();
               micThread = null;
           }
       }

        @Override
        protected void onPause() {
            stopRespondingToMic();
        }

       private class micRunnable implements Runnable {

            @Override
            public void run() {
                Canvas myCanvas;
                Paint paint = new Paint();
                paint.setARGB(255, 255, 100, 100);

                short sData[] = new short[BUFFSIZE/2];
                while (true)
                {
                    int read = recorder.read(sData, 0, BUFFSIZE/2);
                    int min = 0;
                    int max = 0;
                    for (int j=0; j< read; j++)
                    {
                        if (sData[j] < min) min = sData[j];
                        if (sData[j] > max) max = sData[j];
                    }
                    myCanvas = mySurface.getHolder().lockCanvas();
                    myCanvas.drawColor(Color.WHITE);
                    myCanvas.drawCircle(myCanvas.getWidth()/2, myCanvas.getHeight()/2, ((myCanvas.getHeight()/2)*Math.abs(max-min)/65000), paint);
                    mySurface.getHolder().unlockCanvasAndPost(myCanvas);
                }
            }
       }