zoukankan      html  css  js  c++  java
  • 电话录音机

    在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> 
  • 相关阅读:
    数据结构一
    MVC5.0(一)
    异步多线程(六)lock锁
    异步多线程(五)多线程异常处理
    异步多线程(四)Task
    paypal payflow设置视频教程
    Java栈Stack知识点
    Java知识树梳理
    js定时器
    jdk环境变量配置改变不生效的问题
  • 原文地址:https://www.cnblogs.com/SoulCode/p/6393358.html
Copyright © 2011-2022 走看看