无法停止录音,线程无法正常工作,不幸的是应用程序停止

时间:2013-03-14 10:00:04

标签: android multithreading audio

我正在制作一个用于录制音频的示例程序。我有一个用于启动和停止录制的按钮。我正在运行一个线程,以便我可以录制到30秒。我的问题是在达到30秒之前,如果我点击在stopButton上,我的应用程序停止不幸。现在我没有遇到问题。 以下是我的代码:

public class AudioRecordingActivity extends Activity {
private Button RecordButton;
boolean recording;
MediaRecorder recorder ;
Thread RecordThread;
Handler recordHandler;
private TextView timeText;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    timeText=(TextView)findViewById(R.id.textView1);

    RecordButton=(Button)findViewById(R.id.btnRecord);
    RecordButton.setText("Start Recording");
    RecordButton.getBackground().setColorFilter(new LightingColorFilter(-16711936, 0)); 
    recording=false;
    RecordButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (recording)
            {

                 recording=false;

                stopRecording();
                RecordButton.setText("Start Recording");
                RecordButton.getBackground().setColorFilter(new LightingColorFilter(-16711936, 0)); 
             // Toast.makeText(getApplicationContext(), "Recording Stopped", Toast.LENGTH_LONG).show();



            }//if(recording)
            else if(!recording) {

                recording=true;
                startRecording();
                RecordButton.setText("Stop Recording");
                RecordButton.getBackground().setColorFilter(new LightingColorFilter(-65536, 0));
            //    Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();

                recordHandler = new Handler();
                  RecordThread=new Thread(new Runnable() {
                      int j;
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub

                        for( j=30;j>=0;j--){
                            if(recording){
                                recordHandler.post(new Runnable() {

                                    @Override
                                    public void run() {
                                        // TODO Auto-generated method stub
                                    setTimer(j);
                                    }//handler run

                                });//post
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }//catch
                            }else if (!recording)
                            {
                                stopRecording();

                            }
                        }//for
                    }//run  

                });
                RecordThread.start();


            }


        }
    });


}//onCreate
protected void startRecording() {
    // TODO Auto-generated method stub

    Random generator = new Random();
    int n = 1000;
    n = generator.nextInt(n);
     File folder = new File(Environment.getExternalStorageDirectory().toString()+"/AudioRecord");
      if(!folder.exists())
        {
          folder.mkdirs();

        } 
       String path = Environment.getExternalStorageDirectory().getAbsolutePath() +
            "/AudioRecord/audiorecord"+n+".3gp";                  



    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFile(path);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

     Toast.makeText(getApplicationContext(),"Saving Audio as"+ path,
            Toast.LENGTH_LONG).show();
      try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), "IllegalStateException called", Toast.LENGTH_LONG).show();


    } catch (IOException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), "prepare() failed", Toast.LENGTH_LONG).show();

    }

      recorder.start();


}

 private void stopRecording() {

     recorder.stop();
     recorder.release();
     recorder = null;


    }

    private void setTimer(int j) {
        // TODO Auto-generated method stub
         ((TextView)findViewById(R.id.textView1)).setText(String.valueOf(j) + "sec");
        if(j==0){
            recording=false;

        }


    }
}//class end

音频录制完美。我猜问题是停止音频和线程。我不知道要在哪里进行校正。 提前帮助谢谢。

1 个答案:

答案 0 :(得分:0)

问题解决了:经过更正后......

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (recording)
            {
                if(recorder!=null){
                     recorder.stop();
                     recorder.release();
                     recorder = null;
                }

                recording=false;
                RecordButton.setText("Start Recording");
                RecordButton.getBackground().setColorFilter(new LightingColorFilter(-16711936, 0)); 
             // Toast.makeText(getApplicationContext(), "Recording Stopped", Toast.LENGTH_LONG).show();



            }

...

private void setTimer(int j) {
        // TODO Auto-generated method stub
         timeText.setText(String.valueOf(j) + "sec"); 
         if ((recording) && (j==1)){

                recorder.stop();
                recorder.release();
                recorder = null;
                recording=false;
                RecordButton.setText("Start Recording");
                RecordButton.getBackground().setColorFilter(new LightingColorFilter(-16711936, 0)); 

            }





    }