Android电话录音
本例是一个通用的电话录音方案,先创建一个广播接收,
package zhangphil.phonerecorder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class PhoneBroadcastReceiver extends BroadcastReceiver {// BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent) {
// 外拨电话
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
System.out.println(Intent.ACTION_NEW_OUTGOING_CALL);
// 获得外拨号码
String EXTRA_PHONE_NUMBER = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d(this.getClass().getName(), EXTRA_PHONE_NUMBER);
// 开始启动后台服务录音
Intent service = new Intent(context, PhoneService.class);
context.startService(service);
}
}
}
注册到AndroidManifest.xml里面,然后开机启动(通过设置广播过滤器):
<action android:name="android.intent.action.BOOT_COMPLETED" />
同时增加外拨电话的广播过滤器:
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
只要有外拨电话就启动后台服务,然后录制通话录音。录音文件存放在SD卡的根目录下。全程将使用PhoneStateListener监听电话通话状态,通话开始然后就开启MediaRecorder录音,通话结束,然后就使MediaRecorder停止录音。
package zhangphil.phonerecorder;
import java.io.File;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.os.Vibrator;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class PhoneService extends Service {
private File file;
// 录音的格式
private String fileName = "zhangphil.3gp";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
file = new File(path, fileName);
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new MyPhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyPhoneListener extends PhoneStateListener {
private MediaRecorder mediaRecorder;
// 震动
private Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: // 来电
break;
case TelephonyManager.CALL_STATE_OFFHOOK: // 接通电话
startRecordPhoneSound();
break;
// 没有通话(通话结束)
case TelephonyManager.CALL_STATE_IDLE:
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
// 震动
vibrator.vibrate(100);
}
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void startRecordPhoneSound() {
mediaRecorder = new MediaRecorder();
// 这里选麦克风作为录音的声源
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 录音文件的存放位置
mediaRecorder.setOutputFile(file.getAbsolutePath());
try {
mediaRecorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mediaRecorder.start();
// 震动
vibrator.vibrate(100);
}
}
}
需要注意的是,这个电话录音,是通过麦克风录制通话录音的,录制的效果一般。
AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zhangphil.phonerecorder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name=".PhoneService" >
</service>
<receiver android:name=".PhoneBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<!-- 拨打电话权限 -->
<uses-permission android:name="android.permission.CALL_PHONE" />
<!-- 读取电话状态权限 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- SD卡读写权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 录音权限 -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- 震动权限 -->
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
</manifest>