请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
10年前发布

Android实现电话录音

在清单文件AndroidManifest.xml中添加权限:      <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"/>      <!-- 访问internet权限 -->      <uses-permission android:name="android.permission.INTERNET"/>      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

public class PhoneListenerService extends Service {  @Override  public void onCreate() {          TelephonyManager manager =                     (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);          //监听电话的状态      manager.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE);  }    private final class MyListener extends PhoneStateListener {      private String num;      private MediaRecorder recorder;      public void onCallStateChanged(int state, String incomingNumber) {          switch (state) {              case TelephonyManager.CALL_STATE_RINGING:  /* 电话进来时 */                  num = incomingNumber;                  break;              case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起电话时 */                  try {                      File file = new File(Environment.getExternalStorageDirectory(), num + "_" + System.currentTimeMillis() + ".3gp");                      recorder = new MediaRecorder();                      recorder.setAudioSource(AudioSource.MIC);//声音采集来源(话筒)                      recorder.setOutputFormat(OutputFormat.THREE_GPP);//输出的格式                      recorder.setAudioEncoder(AudioEncoder.AMR_NB);//音频编码方式                      recorder.setOutputFile(file.getAbsolutePath());//输出方向                      recorder.prepare();                      recorder.start();                  } catch (Exception e) {                      e.printStackTrace();                  }                  break;              case TelephonyManager.CALL_STATE_IDLE:  /* 无任何状态时 */                  if (recorder != null) {                      recorder.stop();                      recorder.release();                  }                  break;          }      }  }  }