复制代码代码如下:
حزمة com.example.testaudio ؛
استيراد java.io.file ؛
استيراد Android.App.Activity ؛
استيراد Android.media.mediaplayer ؛
استيراد Android.media.mediaplayer.oncompletionListener ؛
استيراد Android.media.mediarecorder ؛
استيراد Android.os.bundle ؛
استيراد Android.OS.Envirialment ؛
استيراد Android.os.handler ؛
استيراد Android.OS.Message ؛
استيراد Android.Util.log ؛
استيراد Android.view.menu ؛
استيراد Android.view.motionevent ؛
استيراد Android.view.view ؛
استيراد Android.view.view.onclickListener ؛
استيراد Android.view.view.ontouchListener ؛
استيراد Android.widget.button ؛
استيراد Android.widget.textView ؛
يمتد النشاط الرئيسي للطبقة العامة النشاط {
private mediArecorder redoder = null ؛
لاعب MediaPlayer الخاص = NULL ؛
السلسلة الخاصة themediaPath ؛
TextView TV = NULL ؛
TextView TVRecord = null ؛
زر testbtn = null ؛
زر testbtn2 = null ؛
Button StopBTN = NULL ؛
زر playbtn = فارغة ؛
@تجاوز
oncreate void المحمي (حزمة SaveInstancestate) {
super.oncreate (SaveInstancestate) ؛
setContentView (r.layout.activity_main) ؛
TV = (TextView) FindViewById (R.Id.TextView1) ؛
tvRecord = (textView) findViewById (r.id.tvrecord) ؛
testBtn = (button) findViewById (r.id.button1) ؛
testBtn2 = (زر) FindViewById (r.id.button2) ؛
STOPBTN = (زر) FindViewById (r.id.buttonstop) ؛
playBtn = (button) findViewById (r.id.buttonplay) ؛
TestBtn2.SetOnCouchListener (جديد OnTouchListener () {
@تجاوز
Ontouch المنطقية العامة (عرض V ، حدث MotionEvent) {
log.i ("testActivity" ، "setOnCouchListener:"+event.getAction ()) ؛
Switch (event.getAction ()) {
حالة motionevent.action_up: {
log.i ("testactivity" ، "停止录音") ؛
StopRecording () ؛
استراحة؛
}
حالة motionevent.action_down: {
log.i ("testactivity" ، "开始录音") ؛
StarTrecording () ؛
استراحة؛
}
الافتراضي: استراحة ؛
}
العودة كاذبة
}
}) ؛
testbtn.setonClickListener (New OnClickListener () {
public void onclick (عرض Arg0) {
StarTrecording () ؛
testbtn.setEnabled (false) ؛
stopBtn.SetEnabled (true) ؛
}
}) ؛
stopBtn.SetEnabled (false) ؛
StopBTN.SetOnClickListener (New OnClickListener () {
public void onclick (عرض Arg0) {
StopRecording () ؛
testbtn.setEnabled (true) ؛
PlayBtn.SetEnabled (true) ؛
stopBtn.SetEnabled (false) ؛
}
}) ؛
playbtn.setonClickListener (new onClickListener () {
public void onclick (عرض Arg0) {
PlayRecordFile (themediaPath) ؛
stopBtn.SetEnabled (true) ؛
}
}) ؛
}
PlayRecordFile المحمي Void PlayRecordFile (سلسلة _file) {
يحاول {
ملف f = ملف جديد (_file) ؛
if (! f.exists ()) {
tv.settext ("文件不存在:" + _file) ؛
يعود؛
}
} catch (استثناء e) {
log.i ("testactivity" ، e.getMessage ()) ؛
}
يحاول {
player = new MediaPlayer () ؛
player.setDataSource (_file) ؛
player.prepare () ؛
player.setoncompletionListener (جديد OnCompleTionListener () {
public void onCompletion (MediaPlayer Arg0) {
tv.settext ("播放完毕") ؛
stopBtn.SetEnabled (false) ؛
}
}) ؛
player.start () ؛
} catch (استثناء e) {
log.e ("testactivity" ، "فشل اللعب:" + e.getMessage ()) ؛
}
}
/**
* 停止录音处理
*/
محمي باطل stopRecording () {
if (redoder! = null) {
log.i ("testactivity" ، "停止录音") ؛
recoder.stop () ؛
recoder.Release () ؛
recoder = null ؛
endtime = system.currentTimeMillis () ؛
_HandlerCordComplete () ؛
}
if (player! = null) {
log.i ("testactivity" ، "停止播放") ؛
player.stop () ؛
player.Release () ؛
لاعب = فارغ ؛
}
}
/**
* 开始录音处理
*/
void void startrecording () {
temediaPath = execute.getExternalStoragedIrectory (). getabsolutepath () ؛
temediaPath += "/audiotest.3gp" ؛
recoder = new MediArecorder () ؛
recoder.setaudiosource (MediArecorder.audiosource.mic) ؛
recoder.setoutputformat (mediArecorder.outputformat.three_gpp) ؛
recoder.setOutputFile (themediaPath) ؛
recoder.setaudioencoder (mediArecorder.audioencoder.amr_nb) ؛
startTime = system.currentTimeMillis () ؛
updatemicstatus () ؛
يحاول {
recoder.prepare () ؛
recoder.start () ؛
_HandlerCordStart () ؛
} catch (استثناء e) {
log.e ("testActivity" ، "فشل التحضير:" + e.getMessage ()) ؛
_HandlerCordStarror (E) ؛
}
}
// 定时器
static static long maxtime = 30*1000 ؛ // 30 秒
وقت البدء الطويل الخاص = 0L ؛
خاص نهاية الانتهاء = 0L ؛
المعالج النهائي الخاص Mandler = New Handler () ؛
mupdatemicStatustimer الخاص بـ Runnable Runnable = جديد RunNable () {
تشغيل الفراغ العام () {
// 判断是否超时
if (starttime> 0 && system.currentTimeMillis () - starttime> maxtime) {
log.e ("testactivity" ، "超时的录音时间 , 直接停止") ؛
StopRecording () ؛
يعود؛
}
// 更新分贝状态
updatemicstatus () ؛
}
} ؛
/**
* 更新话筒状态 分贝是也就是相对响度 分贝的计算公式 k = 20lg (vo/vi) vo 当前振幅值 vi 基准值为 600 : 我是怎么制定基准值的呢? 当 20
* * math.log10 (mmediarecorder.getMaxAmplitude () / vi) == 0 的时候 vi 就是我所需要的基准值
* 当我不对着麦克风说任何话的时候 , 测试获得的 mmediarecorder.getmaxamplitude () 值即为基准值。
* log.i ("mic_" ، "麦克风的基准值 :" + mmediarecorder.getMaxAmplitude ()) ؛ 前提时不对麦克风说任何话
*/
قاعدة int الخاصة = 600 ؛
مساحة int الخاصة = 300 ؛ // 间隔取样时间
private void updatemicstatus () {
if (redoder! = null) {
// int vusize = 10 * mmediarecorder.getMaxAmplitude () / 32768 ؛
نسبة int = recoder.getMaxAmplitude () / base ؛
int db = 0 ؛ // 分贝
إذا (نسبة> 1)
db = (int) (20 * math.log10 (نسبة)) ؛
_HandlerCordVoice (dB) ؛
mhandler.postdelayed (mupdatemicstatustimer ، الفضاء) ؛
/*
* if (db> 1) {vusize = (int) (20 * math.log10 (db)) ؛ log.i ("MIC_" ،
* ":" + vusize) ؛ } آخر log.i ("mic_" ، "麦克风的音量的大小 :" + 0) ؛
*/
}
}
private void _handlerEcordStart () {
// 开始录音的接收函数
tv.settext ("开始录音 ...") ؛
// startTime 开始时间
}
private void _handlerCordStarterror (استثناء E) {
// 开始录音的接收函数失败
tv.settext ("开始录音失败:" + e.getMessage ()) ؛
}
private void _handlerecordcomplete () {
// 结束录音
tv.settext ("停止录音:" + temediaPath) ؛
}
private void _handlerEcordVoice (int _db) {
// 声音事件侦听 , 转换成分贝
tvRecord.settext (""+_ db) ؛
}
@تجاوز
onCreateOptionSmenu (قائمة القائمة) المنطقية العامة (قائمة القائمة) {
// تضخيم القائمة ؛ هذا يضيف عناصر إلى شريط الإجراء في حالة وجوده.
getMenuinflater (). تضخيم (R.Menu.Main ، القائمة) ؛
العودة صحيح.
}
}