在这个时候我遇到了一个不同的问题,我有一个应用程序来录制呼叫,但是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();
按下a按钮拨打号码时执行这部分代码。但是,当我在AVD中按下endcall按钮时,此代码永远不会运行:
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);
仅供参考:第一段代码在辅助类中,第二段在活动中。
我希望有人可以帮助我。 谢谢大家!
答案 0 :(得分: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 ...... !!");
}
}
}