android 录音机服务开发,android服务之录音功能

packagexidian.dy.com.chujia;importandroid.app.Service;importandroid.content.Intent;importandroid.media.MediaRecorder;importandroid.os.Environment;importandroid.os.IBinder;importandroid.support.annotation.Nullable;importandroid.telephony.PhoneStateListener;importandroid.telephony.TelephonyManager;importandroid.util.Log;importjava.io.IOException;/*** Created by dy on 2016/7/12.*/

public class MyService extendsService {

TelephonyManager tm;

@Overridepublic voidonCreate() {super.onCreate();//获取电话管理器

tm =(TelephonyManager) getSystemService(TELEPHONY_SERVICE);//对感兴趣的事件进行监听,传入回调函数

tm.listen(newMyListener(),PhoneStateListener.LISTEN_CALL_STATE);

}class MyListener extendsPhoneStateListener{

MediaRecorder mRecorder;//一旦电话状态改变该方法被调用

@Overridepublic void onCallStateChanged(intstate, String incomingNumber) {super.onCallStateChanged(state, incomingNumber);switch(state){//电话处于空闲状态停止录音

caseTelephonyManager.CALL_STATE_IDLE:if(mRecorder != null){

mRecorder.stop();

mRecorder.release();

mRecorder= null;

}break;//电话处于响铃状态

caseTelephonyManager.CALL_STATE_RINGING:

mRecorder= newMediaRecorder();

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mRecorder.setOutputFile(Environment.getExternalStorageDirectory().toString()+ "/record.3gp");

mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);try{

mRecorder.prepare();

}catch(IOException e) {

Log.e(this.getClass().getName(), "prepare() failed");

}break;//电话处于摘机状态

caseTelephonyManager.CALL_STATE_OFFHOOK:if(mRecorder != null){

mRecorder.start();

}break;

}

}

}

@Nullable

@OverridepublicIBinder onBind(Intent intent) {return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值