zoukankan      html  css  js  c++  java
  • android88 录音机

    package com.itheima.recorder;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    
    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);
        }
    }
    package com.itheima.recorder;
    
    import java.io.IOException;
    
    import android.app.Service;
    import android.content.Intent;
    import android.media.MediaRecorder;
    import android.os.IBinder;
    import android.telephony.PhoneStateListener;
    import android.telephony.TelephonyManager;
    
    public class RecorderService extends Service {
        private MediaRecorder recorder;//录音api
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            //拿到电话管理器
            TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
            //监听电话状态
            //events:决定PhoneStateListener侦听什么内容
            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:
                    System.out.println("空闲");
                    if(recorder != null){
                        recorder.stop();
                        recorder.release();//使用硬件录音是用c语言占用资源是不会自动释放的需要手动释放
                        recorder = null;
                    }
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    System.out.println("响铃");
                    if(recorder == null){
                        recorder = new MediaRecorder();
                        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//音频来源麦克风
                        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        recorder.setOutputFile("sdcard/luyin.3gp");//输出文件路径
                        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置音频编码
                        try {
                            recorder.prepare();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    System.out.println("摘机");
                    //开始录音
                    if(recorder != null){
                        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" /> 写sd卡权限
      <uses-permission android:name="android.permission.RECORD_AUDIO" />
      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>获取开机启动
        
        <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>
            <receiver android:name="com.itheima.recorder.BootReceiver">
                <intent-filter >
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>开机广播接收者
                </intent-filter>
            </receiver>
        </application>
    
    </manifest>
  • 相关阅读:
    Angular指令2
    MD测试
    LeeCX
    Java公众号推荐
    基于云落地SLB+Tengine实现高可用集群负载均衡
    非HTTP状态下开启web视频的临时方案
    慕课网 20200330 es+geo+baidu-map 直播视频与文字版笔记
    [涨姿势]Chrome 谷歌浏览器原生截长图命令
    人人都要关注的分布式系统中的CAP理论
    疫情风险下的项目管理与紧急预案策略
  • 原文地址:https://www.cnblogs.com/yaowen/p/4948835.html
Copyright © 2011-2022 走看看