本文实例讲述了android编程实现电话录音的方法。分享给大家供大家参考。具体如下:
在清单文件AndroidManifest.xml中添加权限:
1
2
3
4
5
6
7
8
9
|
< 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" /> |
java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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 ; } } } } |
希望本文所述对大家的Android程序设计有所帮助。