电话录音机

在MainActivity中
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View v){
        Intent intent = new Intent(this, RecorderService.class);
        startService(intent);
    }
    
}
 
RecorderService 
public class RecorderService extends Service {
    private MediaRecorder recorder;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        //获取电话管理器
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        //监听电话状态
        tm.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE);
    }
    
    class MyListener extends PhoneStateListener{
        
        //电话状态改变时回调
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            //判断当前是什么状态
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                if(recorder != null){
                    System.out.println("停止录音");
                    //停止录音
                    recorder.stop();
                    //释放录音机占用的资源
                    recorder.release();
                    recorder = null;
                }
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                if(recorder == null){
                    recorder = new MediaRecorder();
                    //设置音频来源
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    //设置输出格式
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    recorder.setOutputFile("sdcard/voice.3gp");
                    //设置音频编码
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    try {
                        System.out.println("准备好");
                        recorder.prepare();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                }
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                if(recorder != null){
                    System.out.println("开始录音");
                    recorder.start();
                }
                break;
            }
        }
        
    }
}
在清单文件中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.recorder"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.RECORD_AUDIO" />3
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima.recorder.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.itheima.recorder.RecorderService"></service>
    </application>
</manifest> 
posted @ 2017-02-13 13:21  SoulCode  阅读(227)  评论(0编辑  收藏  举报