录制语音(仅MIC),当电话呼入或呼出呼出、语音、电话、呼入

2023-09-06 18:01:57 作者:尤物的陷阱

我遵循的Andr​​oid手机上记录电话的例子吗?并把它放在一个广播接收器以试图录制MIC的声音(我知道这仍仅限于记录对方),当电话呼入和呼出。我的问题是:我如何能得到国家,当用户拿起电话。因为当它响起来,这也将去的android.intent.action.PHONE_STATE的动作。

I follow the example of Record phone calls on android phone? and put it on a BroadcastReceiver to try to record MIC voice (I know it is still limited to record the other side) when phone call incoming and outgoing. My question is: how can I get the state when the user pick up the phone. Because when it is ringing, it also will go to the action of "android.intent.action.PHONE_STATE".

我的code:

public class PhoneCallReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        String action = intent.getAction();
        if (action.equals("android.intent.action.PHONE_STATE") 
        {
            // Phone call recording
            try {
                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                recorder.setOutputFile(<my output dir>);
                recorder.prepare();
                recorder.start();
                recordStarted = true;
                telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
            } catch(Exception ex) {

            }
        } 
    }
}


private final PhoneStateListener phoneListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        try {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING: {
                    // 
                    break;
                }
                case TelephonyManager.CALL_STATE_OFFHOOK: {
                    // 
                    break;
                }
                case TelephonyManager.CALL_STATE_IDLE: {
                    if (recordStarted) {
                        recorder.stop();
                        recordStarted = false;
                    }
                    break;
                }
                default: { }
            }
        } catch (Exception ex) {
        }
    } 
};

在code在AndroidManifest.xml

the code in AndroidManifest.xml

<receiver android:name=".PhoneCallReceiver" android:enabled="true">
    <intent-filter android:priority="1000">
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

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

基于Android 2.1的SDK和HTC EVO 4G(Android 2.2的)测试

推荐答案

您可以使用android.intent.action.ANSWER行动,而不是使用android.intent.action.PHONE_STATE的动作。您可以录制语音当呼叫应答。

you can use the android.intent.action.ANSWER action instead of using the android.intent.action.PHONE_STATE" action. you are able to record the voice when the call is answered.