一:通话录音状态广播注册
<receiver android:name="com.example.receiver.PhoneReceiver" >
<intent-filter android:priority="1000" >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.ACTION_NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
二:通话录音代码
public class PhoneReceiver extends BroadcastReceiver {
public static final String INTENT_PHONE_STR = "phone";
public static final String INTENT_STATE_BOOL = "state";
//手机号
private static String phone;
//是否拨打电话
private static boolean isDial;
@Override
public void onReceive(Context context, Intent intent) {
// 数据为空处理
if (intent == null) {
intent = new Intent();
}
if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) {// 打出号码
String curPhone = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
phone = curPhone;
isDial = true;
return;
}
// 电话监听
TelephonyManager telMgr = (TelephonyManager) context
.getSystemService(Service.TELEPHONY_SERVICE);
switch (telMgr.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:// 打进号码
String number = intent
.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
LogUtil.e("TelephonyManager.CALL_STATE_RINGING" + number);
phone = number;
isDial = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
LogUtil.e("TelephonyManager.CALL_STATE_OFFHOOK");
Intent i = new Intent(context, RecorderService.class);
i.putExtra(INTENT_PHONE_STR, phone);
i.putExtra(INTENT_STATE_BOOL, isDial);
context.startService(i);
break;
case TelephonyManager.CALL_STATE_IDLE:
LogUtil.e("TelephonyManager.CALL_STATE_IDLE");
context.stopService(new Intent(context, RecorderService.class));
break;
}
}
三:通话录音服务
public class RecorderService extends Service {
public static final String INTENT_PATH = "path";
/**
*/
private String currentFile = "";
/**
*
* @param incomingNumber
*/
private MediaRecorder recorder;
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String phone = intent.getStringExtra(PhoneReceiver.INTENT_PHONE_STR);
boolean isDial = intent.getBooleanExtra(
PhoneReceiver.INTENT_STATE_BOOL, true);
// 创建文件
String dirPath = FileUtil.getBasePath(getApplicationContext());
File ff = new File(dirPath);
if (!ff.exists()) {
ff.mkdirs();
}
currentFile = dirPath + File.separator + getFileName(phone, isDial);
setMaxStreamVolume();
iniRecord(1);
return super.onStartCommand(intent, flags, startId);
}
/**
* 设置最大的通话音量。
*/
private void setMaxStreamVolume() {
AudioManager mAudioManager = ((AudioManager) getSystemService(Context.AUDIO_SERVICE));
int maxVolume = mAudioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
maxVolume, 0);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);
}
/**
* 初始化录音
*/
private void iniRecord(int type) {
try {
recorder = new MediaRecorder();
if (type == 1) {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);// 通话双向录音
} else if (type == 2) {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 麦克风
} else if (type == 3) {
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);// 耳机录音
} else if (type == 4) {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
} else if (type == 5) {
recorder.setAudioSource(MediaRecorder.AudioSource.REMOTE_SUBMIX);
}
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setMaxDuration(1000 * 60 * 60 * 24);
recorder.setMaxFileSize(1024 * 1024 * 1024 * 1024);
recorder.setOutputFile(currentFile);
Thread.sleep(2000);
recorder.prepare();
recorder.start();
Log.e("phoneNumber", "录音开始" + currentFile);
} catch (Throwable e) {
Log.e("phoneNumber", "录音error");
e.printStackTrace();
if (type <= 4) {
iniRecord(type + 1);
} else {
iniRecord(1);
}
}
}
/**
* 停止录音
*/
private void stopRecording() {
if (recorder != null) {
try {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
} catch (Exception e) {
}
Intent i = new Intent(ListActivity.RECORDER_DATA);
i.putExtra(INTENT_PATH, currentFile);
getApplication().sendBroadcast(i);
}
}
/**
* 获取名称
*
* @param incomingNumber
* @return
*/
private String getFileName(String incomingNumber, boolean isDial) {
Date date = new Date(System.currentTimeMillis());
String name;
if (isDial) {
name = incomingNumber + "_" + DateUtil.format(date) + "_" + "1";
} else {
name = incomingNumber + "_" + DateUtil.format(date) + "_" + "2";
}
return name;
}
/**
* onDestroy
*/
@Override
public void onDestroy() {
super.onDestroy();
stopRecording();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
四:效果图:
五:代码地址:https://download.youkuaiyun.com/download/u014416317/10958959