2012-01-23 30 views
4

我製作了一個應用程序,可以記錄聲音並分析頻率。這個過程每秒重複幾次,從而使用線程。在Android中錄製聲音時的錯誤

這在大部分時間都有效,但由於某種原因,在logcat中,我在第一次分析後重復了這些消息。

很少(但有時)當我測試時,應用程序沒有聲音。所以我認爲它與這個錯誤有關。

01-23 13:52:03.414: E/AudioRecord(3647): Could not get audio input for record source 1 
01-23 13:52:03.424: E/AudioRecord-JNI(3647): Error creating AudioRecord instance: initialization check failed. 
01-23 13:52:03.424: E/AudioRecord-Java(3647): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object. 

代碼如下,有沒有人有任何想法,我即將出錯?我沒有正確地殺死AudioRecord對象嗎?代碼已被修改爲便於閱讀:

public class recorderThread extends AsyncTask<Sprite, Void, Integer> { 

short[] audioData; 
int bufferSize; 

@Override 
protected Integer doInBackground(Sprite... ball) { 

     boolean recorded = false; 
     int sampleRate = 8192; 
     AudioRecord recorder = instatiateRecorder(sampleRate); 


     while (!recorded) { //loop until recording is running 

     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 {    
      //read the PCM audio data into the audioData array 

       //get frequency 
       //checks if correct frequency, assigns number 
       int correctNo = correctNumber(frequency, note); 

       checkIfMultipleNotes(correctNo, max_index, frequency, sampleRate, magnitude, note); 

       if (audioDataIsNotEmpty()) 
        recorded = true; 

        return correctNo; 
       } 
     } 
     else 
     { 
      recorded = false; 
      recorder = instatiateRecorder(sampleRate); 
     } 
    } 

     if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING) 
     { 
      killRecorder(recorder); 
     } 

     return 1; 
} 


private void killRecorder(AudioRecord recorder) { 
    recorder.stop(); //stop the recorder before ending the thread 
    recorder.release(); //release the recorders resources 
    recorder=null; //set the recorder to be garbage collected 
} 




@Override 
protected void onPostExecute(Integer result) { 
    ballComp.hitCorrectNote = result;  
} 

private AudioRecord instatiateRecorder(int sampleRate) {  
     bufferSize= AudioRecord.getMinBufferSize(sampleRate,AudioFormat.CHANNEL_CONFIGURATION_MONO, 
       AudioFormat.ENCODING_PCM_16BIT)*2; 
//get the buffer size to use with this audio record 

     AudioRecord recorder = new AudioRecord (AudioSource.MIC,sampleRate,AudioFormat.CHANNEL_CONFIGURATION_MONO, 
       AudioFormat.ENCODING_PCM_16BIT,bufferSize); 
//instantiate the AudioRecorder 

     audioData = new short [bufferSize]; 
//short array that pcm data is put into.   
     return recorder; 
} 
} 
+0

你以前'登錄您的狀態,如果(recorder.getState()== android.media.AudioRecord.RECORDSTATE_RECORDING)'所以你沒有'STATE_INITIALIZED'的情況?猜猜可能導致設備未正確釋放。 – tidbeck

+0

https://github.com/vinodselvin/ViCorder/ –

回答

1

什麼阻止你有兩個RecorderThreads在同一時間運行?顯示實例化這些對象之一的代碼,執行它,並且當然等待任何先前的RecorderThread先完成。

如果答案是什麼停在同一時間運行兩個RecorderThreads,那麼你的「靜態」的使用顯然是一個問題......第二個線程將導致第一AudioRecord,同時打開被泄露。恕我直言,嘗試避免使用靜態數據是個好主意。

+0

我已經刪除了靜態變量並更新了代碼,發生了同樣的錯誤,有什麼想法? –

+0

您可以嘗試回答我的問題:「什麼使您有兩個RecorderThreads同時運行?」 –

+0

那麼代碼工作,所以沒有,但errore仍然出現在logcat,所以我問的是什麼 –

2

當你的日誌說「無法獲取音頻輸入的記錄源1」,這意味着。 Android設備未找到任何用於錄製聲音的硬件。

因此,如果您正在從模擬器測試應用程序,那麼請確保在錄製聲音的過程中成功連接了鼠標,或者如果您正在從設備調試或運行它,請確保麥克風處於錄製狀態聲音。

希望它能幫助你。

或者

如果上面沒有解決您的問題,然後使用下面的代碼來記錄聲音,因爲它的工作非常適合我。

代碼:

record.setOnClickListener(new View.OnClickListener() 
    { 
     boolean mStartRecording = true; 
     public void onClick(View v) 
     { 

       if (mStartRecording==true) 
       { 
        //startRecording(); 
        haveStartRecord=true; 
        String recordWord = wordValue.getText().toString(); 
        String file = Environment.getExternalStorageDirectory().getAbsolutePath(); 
        file = file+"/"+recordWord+".3gp"; 

        System.out.println("Recording Start"); 
        //record.setText("Stop recording"); 
        record.setBackgroundDrawable(getResources().getDrawable(R.drawable.rec_on)); 
        mRecorder = new MediaRecorder(); 
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
        mRecorder.setOutputFile(file); 
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 

// mRecorder.setAudioChannels(1); // mRecorder.setAudioSamplingRate(8000);

    try 
        { 
         mRecorder.prepare(); 
        } 
        catch (IOException e) 
        { 
         Log.e(LOG_TAG, "prepare() failed"); 
        } 

        mRecorder.start(); 

       } 

       else 
       { 
        //stopRecording(); 
        System.out.println("Recording Stop"); 
        record.setBackgroundDrawable(getResources().getDrawable(R.drawable.rec_off)); 
        mRecorder.stop(); 
        mRecorder.release(); 
        mRecorder = null; 
        haveFinishRecord=true; 

       } 
       mStartRecording = !mStartRecording; 

     } 
    }); 

希望此答案對您有所幫助。

享受。 :)

+0

這是否適用於即時錄音?進入緩衝區而不是文件?感謝您的幫助 –

+0

@BenTaliadoros:你是什麼意思?我已經在真實的設備上測試過它,並且也進入了仿真器。它對我來說非常適合。請隨時留下評論mwans錄音。 –

+0

@BenTaliadoros:我在等你的回覆。 。 。 –

1

我有同樣的問題。我解決它通過添加

"<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>" 

的 「AndroidManifest.xml中」