0

我正在使用以下代码。它工作正常并显示频率。但是每次在启动应用程序之前,我都需要重新安装它才能正常工作。会不会是对象没有被释放?我在这里包括我的代码。

public class Test extends Activity{

String TAG;
public boolean recording;
AudioRecord recorder;
int numCrossing,p;
short audioData[];
int bufferSize,srate;
TextView disp;
float frequency;
private static int[] sampleRate = new int[] { 44100, 22050, 11025, 8000 };

@Override
protected void onCreate(Bundle Testing) {
    // TODO Auto-generated method stub
    super.onCreate(Testing);
    setContentView(R.layout.activity_start);
    disp = (TextView) findViewById (R.id.tvDisp);

    Thread t1 = new Thread(new Runnable(){

        public void run() {

            Log.i(TAG,"Setting up recording");
            for (int rate : sampleRate) {
                try{

                        Log.d(TAG, "Attempting rate " + rate);

            bufferSize=AudioRecord.getMinBufferSize(rate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT)*3; //get the buffer size to use with this audio record

            if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {

            recorder = new AudioRecord (MediaRecorder.AudioSource.MIC,rate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT,bufferSize); //instantiate the AudioRecorder
            srate = rate;
            break;
            }

            } catch (Exception e) {
                Log.e(TAG, rate + "Exception, keep trying.",e);
            }
        }


            recording=true; //variable to use start or stop recording
            audioData = new short [bufferSize]; //short array that pcm data is put into.
            Log.i(TAG,"Got buffer size =" + bufferSize);                
            while (recording) {  //loop while recording is needed
                   Log.i(TAG,"in while 1");
            if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED) // check to see if the recorder has initialized yet.
            if (recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
            recorder.startRecording();  //check to see if the Recorder has stopped or is not recording, and make it record.

            else {
                   Log.i(TAG,"in else");
            recorder.read(audioData,0,bufferSize); //read the PCM audio data into the audioData array

            numCrossing=0; //initialize your number of zero crossings to 0
            for (p=0;p<bufferSize/4;p+=4) {
                   if (audioData[p]>0 && audioData[p+1]<=0) numCrossing++;
                    if (audioData[p]<0 && audioData[p+1]>=0) numCrossing++;
                    if (audioData[p+1]>0 && audioData[p+2]<=0) numCrossing++;
                    if (audioData[p+1]<0 && audioData[p+2]>=0) numCrossing++;
                    if (audioData[p+2]>0 && audioData[p+3]<=0) numCrossing++;
                    if (audioData[p+2]<0 && audioData[p+3]>=0) numCrossing++;
                    if (audioData[p+3]>0 && audioData[p+4]<=0) numCrossing++;
                    if (audioData[p+3]<0 && audioData[p+4]>=0) numCrossing++;
                    }//for p

              for (p=(bufferSize/4)*4;p<bufferSize-1;p++) {
                    if (audioData[p]>0 && audioData[p+1]<=0) numCrossing++;
                    if (audioData[p]<0 && audioData[p+1]>=0) numCrossing++;
                    }



            frequency=(srate/bufferSize)*(numCrossing/2);//calculating frequency

             Log.i(TAG, "Calculated Frequency");
             runOnUiThread(new Runnable(){
                 public void run()
                 {
                     Log.i(TAG,"In printing statement");
                     disp.setText("The frequency is " + frequency);
                     if(frequency>=15000)
                         recording = false;
                 }
             });


             }//else recorder started


    } //while recording

   if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING) 
    recorder.stop(); //stop the recorder before ending the thread
    recorder.release(); //release the recorders resources
    recorder=null; //set the recorder to be garbage collected.

}//跑

    });
    t1.start();
}

}

4

1 回答 1

0

我没有对您的代码进行详尽的阅读,但我确实看到您仅在频率 >= 15000 时才设置recording = false。您可能不会退出while 循环。

顺便说一句...我相信这就是正在发生的事情...您的线程占用了设备的录制资源。当您退出活动,离开应用程序,线程继续运行。当您尝试再次启动应用程序时,会创建一个新线程,但旧线程仍在运行并仍在占用录制资源。我相信,重新安装应用程序的效果是杀死那个流氓线程。

于 2013-06-08T10:05:49.597 回答