我正在使用以下代码。它工作正常并显示频率。但每次启动应用程序之前,我需要重新安装它,以便它正常工作。可能是对象没有被释放?我在这里包含我的代码。
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();
}
}
答案 0 :(得分:0)
我没有详尽地阅读您的代码,但我确实看到您只在频率&gt; = 15000时才设置录音=假。您可能没有退出您的while循环。
顺便说一句......我相信这就是正在发生的......你的线程会占用你设备的录音资源。当您退出活动,离开应用程序时,线程会继续运行。当您再次尝试启动应用程序时,会创建一个新线程,但旧线程仍在运行,并且仍会占用录制资源。我相信,重新安装应用程序会杀死那个流氓线程。