电话呼入或呼出时录制语音(仅限MIC)

时间:2011-10-28 08:14:39

标签: android broadcastreceiver mediarecorder

我按照Record phone calls on android phone?的示例并将其放在 BroadcastReceiver 上以尝试录制MIC语音(我知道它仍然限于录制另一方),当电话呼入时传出。 我的问题是:当用户拿起电话时,我怎样才能获得状态。因为当它响起时,它也会转到“android.intent.action.PHONE_STATE”的动作。

我的代码:

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) {
        }
    } 
};

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)中测试

2 个答案:

答案 0 :(得分:1)

如果你想录制两端的声音(根据Android Docs)

recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

我没有试过这个,但希望对你有用并回答你的问题,你看过getCallState()的{​​{1}}函数了吗?

答案 1 :(得分:0)

您可以使用android.intent.action.ANSWER操作,而不是使用android.intent.action.PHONE_STATE操作。您可以在应答呼叫时录制语音。