2012-02-10 120 views
0

在這段時間裏,我得到了一個不同的問題,我有一個應用程序來錄製呼叫,但是recorder.start()永遠不會結束。 這裏是用於記錄的代碼:錄音通話永不結束

recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    recorder.setOutputFile(path); 
    recorder.setMaxDuration(1000); 
    recorder.prepare(); 
    recorder.start(); 

這部分代碼執行時按下一個按鈕來調用一個號碼。但是,當我按在AVD結束呼叫按鈕,該代碼永遠不會運行:

 phoneListener = new PhoneStateListener() 
     { 
      @Override 
      public void onCallStateChanged(int state, String incomingNumber) 
      { 
       switch (state) 
       { 
        case TelephonyManager.CALL_STATE_IDLE: 
         currentPhoneState = "CALL_STATE_IDLE"; 
        break; 
        case TelephonyManager.CALL_STATE_OFFHOOK: 
         currentPhoneState = "CALL_STATE_OFFHOOK"; 
        break; 
        case TelephonyManager.CALL_STATE_RINGING: 
         currentPhoneState = "CALL_STATE_RINGING"; 
        break; 
       } 

       _fileTrace.onTrace("INFO", "CallState: ", currentPhoneState, null); 
       if (currentPhoneState == "CALL_STATE_OFFHOOK") 
       {      
        llamada = true; 
        _fileTrace.onTrace("INFO", "Recording Start", currentPhoneState, null); 
        try { 
         recorder.start(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       if (llamada && currentPhoneState == "CALL_STATE_IDLE") { 
        _fileTrace.onTrace("INFO", "CallState: ", currentPhoneState, null); 
        recorder.stop(); 
       }     
      } 
     };  
     _CurrTelephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); 

供參考:第一部分的代碼是一個輔助類和第2個是在活動。

我希望有人能幫助我。 謝謝大家!

+0

一些Android開發人員幫助我嗎? – AuTi 2012-02-13 13:27:30

+0

最後的機會,這個職位的傢伙! – AuTi 2012-02-15 13:40:19

回答

1

我用下面的代碼和它的工作:

public void onReceive(Context context, Intent intent) { 
    // Toast.makeText(context, "calling now", Toast.LENGTH_LONG).show(); 
    if (!intent.getAction().equals("android.intent.action.PHONE_STATE")) 
     return; 
    else { 
     String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 

     if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
      log.d(TAG, "RINGING NOW") 

      return; 
     } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
      StartRecording(); 
      Log.d(TAG, "CALL ANSWERED NOW"); 
      return; 
     } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
      Log.d(TAG, "ALL DONE IN ELSE IF...... !!"); 
      StopRecording(); 
     } else { 
      Log.d(TAG, "ALL DONE IN ELSE ...... !!"); 

     } 
    } 
}