gpt4 book ai didi

android - 来电或去电时录制语音(仅 MIC)

转载 作者:行者123 更新时间:2023-11-29 02:10:14 26 4
gpt4 key购买 nike

我以 Record phone calls on android phone? 为例并将其放在 BroadcastReceiver 上,以尝试在来电和去电时录制 MIC 语音(我知道它仍然仅限于录制另一端)。我的问题是:当用户拿起电话时,我怎样才能得到状态。因为在响铃的时候,它也会去到“android.intent.action.PHONE_STATE”的action。

我的代码:

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

最佳答案

如果你想从两端使用录制语音(根据 Android 文档)

recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

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

关于android - 来电或去电时录制语音(仅 MIC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7926707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com