正文  多媒体 > 录音 >

android实现电话录音

android实现电话录音配置文件[html]<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="codefans.app"...

android实现电话录音

配置文件

 

[html]

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. package="codefans.app"

  4. android:versionCode="1"

  5. android:versionName="1.0" >

  6.  

  7. <uses-sdk android:minSdkVersion="8" />

  8.  

  9. <!-- 监听电话状态权限 -->

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

  11.  

  12. <!-- 在SDCard中创建与删除文件权限 -->

  13. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

  14.  

  15. <!-- 在SDCard写入数据权限 -->

  16. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  17.  

  18. <!-- 刻入声音权限 -->

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

  20. <application

  21. android:icon="@drawable/ic_launcher"

  22. android:label="@string/app_name" >

  23. <service android:name=".PhoneService"></service>

  24. <receiver android:name=".PhoneBroadcastReceiver">

  25. <intent-filter >

  26. <action android:name="android.intent.action.BOOT_COMPLETED"/> <!-- 开机启动广播 -->

  27. </intent-filter>

  28. </receiver>

  29. </application>

  30.  

  31. </manifest>

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="codefans.app" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <!-- 监听电话状态权限 --> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 在SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- 刻入声音权限 --> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <service android:name=".PhoneService"></service> <receiver android:name=".PhoneBroadcastReceiver"> <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED"/> <!-- 开机启动广播 --> </intent-filter> </receiver> </application> </manifest>

[java]

  1. public class PhoneService extends Service

  2. {

  3.  

  4. @Override

  5. public IBinder onBind(Intent intent)

  6. {

  7. // TODO Auto-generated method stub

  8. return null;

  9. }

  10.  

  11. @Override

  12. public void onCreate()

  13. {

  14. super.onCreate();

  15.  

  16. TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

  17.  

  18. telephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE); //注册监听器 监听电话状态

  19. }

  20.  

  21.  

  22. private final class PhoneListener extends PhoneStateListener

  23. {

  24. private String incomeNumber; //来电号码

  25. private MediaRecorder mediaRecorder;

  26. private File file;

  27. @Override

  28. public void onCallStateChanged(int state, String incomingNumber)

  29. {

  30. try

  31. {

  32. switch(state)

  33. {

  34. case TelephonyManager.CALL_STATE_RINGING: //来电

  35. this.incomeNumber = incomingNumber;

  36. break;

  37. case TelephonyManager.CALL_STATE_OFFHOOK: //接通电话

  38. file = new File(Environment.getExternalStorageDirectory(), this.incomeNumber + System.currentTimeMillis() + ".3gp");

  39. mediaRecorder = new MediaRecorder();

  40. mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); //获得声音数据源

  41. mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // 按3gp格式输出

  42. mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

  43. mediaRecorder.setOutputFile(file.getAbsolutePath()); //输出文件

  44. mediaRecorder.prepare(); //准备

  45. mediaRecorder.start();

  46. break;

  47.  

  48. case TelephonyManager.CALL_STATE_IDLE: //挂掉电话

  49. if(mediaRecorder != null)

  50. {

  51. mediaRecorder.stop();

  52. mediaRecorder.release();

  53. mediaRecorder = null;

  54. }

  55.  

  56. break;

  57.  

  58. }

  59. }

  60. catch (IllegalStateException e)

  61. {

  62. // TODO Auto-generated catch block

  63. e.printStackTrace();

  64. }

  65. catch (IOException e)

  66. {

  67. // TODO Auto-generated catch block

  68. e.printStackTrace();

  69. }

  70. }

  71.  

  72.  

  73. }

  74.  

  75.  

  76. }

public class PhoneService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { super.onCreate(); TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE); //注册监听器 监听电话状态 } private final class PhoneListener extends PhoneStateListener { private String incomeNumber; //来电号码 private MediaRecorder mediaRecorder; private File file; @Override public void onCallStateChanged(int state, String incomingNumber) { try { switch(state) { case TelephonyManager.CALL_STATE_RINGING: //来电 this.incomeNumber = incomingNumber; break; case TelephonyManager.CALL_STATE_OFFHOOK: //接通电话 file = new File(Environment.getExternalStorageDirectory(), this.incomeNumber + System.currentTimeMillis() + ".3gp"); mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //获得声音数据源 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // 按3gp格式输出 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setOutputFile(file.getAbsolutePath()); //输出文件 mediaRecorder.prepare(); //准备 mediaRecorder.start(); break; case TelephonyManager.CALL_STATE_IDLE: //挂掉电话 if(mediaRecorder != null) { mediaRecorder.stop(); mediaRecorder.release(); mediaRecorder = null; } break; } } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

[java]

  1. public class PhoneBroadcastReceiver extends BroadcastReceiver

  2. {

  3.  

  4. @Override

  5. public void onReceive(Context context, Intent intent)

  6. {

  7. Intent service = new Intent(context, PhoneService.class);

  8. context.startService(service); //启动服务

  9. }

  10.  

  11. }

public class PhoneBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent service = new Intent(context, PhoneService.class); context.startService(service); //启动服务 } }