码迷,mamicode.com
首页 > 移动开发 > 详细

android88 录音机

时间:2015-11-09 01:39:43      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:

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>

 

android88 录音机

标签:

原文地址:http://www.cnblogs.com/yaowen/p/4948835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!