复制代码代码如下:
pacote com.example.testaudio;
importar java.io.file;
importar Android.App.Activity;
importar android.media.mediaplayer;
importar android.media.mediaplayer.oncompletionListener;
importar android.media.mediarecorder;
importar android.os.bundle;
importar android.os.environment;
importar Android.OS.Handler;
importar Android.OS.Message;
importar android.util.log;
importar android.view.menu;
importar android.view.motionEvent;
importar Android.View.View;
importar Android.View.View.OnClickListener;
importar Android.View.View.ontouchListener;
importar android.widget.button;
importar android.widget.textView;
classe pública MainActivity estende a atividade {
Recoder de mediarecorder privado = nulo;
MediaPlayer Private Player = NULL;
TEMEDIA DE STRING PRIVADO;
TextView tv = null;
TextView tvRecord = null;
Botão testbtn = null;
Botão testbtn2 = null;
Botão stopbtn = null;
Button playbtn = null;
@Override
OnCreate de vazio protegido (pacote salva -seancestado) {
super.oCreate (savedInstancestate);
setContentView (r.layout.activity_main);
TV = (TextView) FindViewById (R.id.TextView1);
TVRecord = (TextView) FindViewById (r.id.tvRecord);
testbtn = (botão) findViewById (r.id.button1);
testbtn2 = (botão) findViewById (r.id.button2);
stopbtn = (botão) findViewById (r.id.buttonstop);
playbtn = (botão) findViewById (r.id.buttonplay);
testbtn2.setontouchListener (new OnTouchListener () {
@Override
public boolean oTouch (View V, MotionEvent Event) {
Log.i ("testatividade", "setontouchListener:"+event.getAction ());
switch (event.getAction ()) {
case MotionEvent.action_up: {
Log.i ("testatividade", "停止录音");
stopRecording ();
quebrar;
}
case MotionEvent.action_down: {
Log.i ("testatividade", "开始录音");
startrecording ();
quebrar;
}
Padrão: quebra;
}
retornar falso;
}
});
testbtn.setOnClickListener (novo OnClickListener () {
public void OnClick (veja arg0) {
startrecording ();
testbtn.setEnabled (false);
stopbtn.setEnabled (true);
}
});
stopbtn.setEnabled (false);
stopbtn.setOnClickListener (new OnClickListener () {
public void OnClick (veja arg0) {
stopRecording ();
testbtn.setEnabled (true);
playbtn.setEnabled (true);
stopbtn.setEnabled (false);
}
});
playbtn.setOnClickListener (novo OnClickListener () {
public void OnClick (veja arg0) {
PlayRecordFile (ThemediaPath);
stopbtn.setEnabled (true);
}
});
}
Protected void playrecordfile (string _file) {
tentar {
Arquivo f = novo arquivo (_file);
if (! f.exists ()) {
TV.Settext ("文件不存在:" + _File);
retornar;
}
} catch (Exceção e) {
Log.i ("testatividade", e.getMessage ());
}
tentar {
player = new MediaPlayer ();
player.setDataSource (_File);
player.Prepare ();
player.setOnCompletionListener (new OncompletionListener () {
public void oncompletion (MediaPlayer arg0) {
tv.settext ("播放完毕");
stopbtn.setEnabled (false);
}
});
player.start ();
} catch (Exceção e) {
Log.e ("testatividade", "play falhou:" + e.getMessage ());
}
}
/**
* 停止录音处理
*/
Void protegido StopRecording () {
if (recodificador! = null) {
Log.i ("testatividade", "停止录音");
recoder.stop ();
recoder.release ();
recodificador = nulo;
endtime = system.currenttimemillis ();
_HandleRecordComplete ();
}
if (player! = null) {
Log.i ("testatividade", "停止播放");
player.stop ();
player.release ();
jogador = nulo;
}
}
/**
* 开始录音处理
*/
Void protegido Startrecording () {
temediaPath = Environment.GetexternalStoragedirectory (). getAbsolutepath ();
temediapath += "/audioTest.3gp";
recodificador = new mediarEcorder ();
recoder.setAudiosource (mediarecorder.audiosource.mic);
recoder.setOutputFormat (mediarecorder.outputFormat.three_gpp);
recoder.setOutputFile (temediaPath);
Recoder.SetAudioEncoder (mediarecorder.audioEncoder.amr_nb);
startTime = system.currenttimemillis ();
updatatemicstatus ();
tentar {
recoder.Prepare ();
recoder.start ();
_HandleRecordStart ();
} catch (Exceção e) {
Log.e ("testatividade", "preparado falhou:" + e.getMessage ());
_HandleRecordStarterror (e);
}
}
// 定时器
Máxtime longo estático privado = 30*1000; // 30 秒
Private Long StartTime = 0L;
Private Long Endtime = 0L;
Manipulador final privado mhandler = new Handler ();
private Runnable mupdatemicStatustimer = new Runnable () {
public void run () {
// 判断是否超时
if (starttime> 0 && system.currenttimemillis () - starttime> maxtime) {
Log.e ("testatividade", "超时的录音时间 , 直接停止");
stopRecording ();
retornar;
}
// 更新分贝状态
updatatemicstatus ();
}
};
/**
* 更新话筒状态 分贝是也就是相对响度 分贝的计算公式 k = 20lg (vo/vi) Vo 当前振幅值 vi 基准值为 600 : 我是怎么制定基准值的呢? 20 20
* * Math.log10 (mmediarecorder.getmaxamplitude () / vi) == 0 的时候 vi 就是我所需要的基准值
* 当我不对着麦克风说任何话的时候 , 测试获得的 mmediarecorder.getmaxamplitude () 值即为基准值。
* Log.i ("mic_", "麦克风的基准值:" + mmediarecorder.getmaxamplitude ()); 前提时不对麦克风说任何话
*/
private int base = 600;
private int espaço = 300; // 间隔取样时间
private void updateMicStatus () {
if (recodificador! = null) {
// int vusize = 10 * mmediarecorder.getmaxamplitude () / 32768;
Int proporção = recoder.getMaxamplitude () / base;
int db = 0; // 分贝
se (proporção> 1)
db = (int) (20 * math.log10 (proporção));
_HandleRecordVoice (dB);
Mhandler.PostDelayed (mupdatemicstatustimer, espaço);
/*
* if (db> 1) {vusize = (int) (20 * math.log10 (db)); Log.i ("mic_",
* "麦克风的音量的大小:" + vusize); } else log.i ("mic_", "麦克风的音量的大小:" + 0);
*/
}
}
Void privado _HandleRecordStart () {
// 开始录音的接收函数
tv.settext ("开始录音 ...");
// StartTime 开始时间
}
private void _HandleRecordStarterror (Exceção e) {
// 开始录音的接收函数失败
TV.Settext ("开始录音失败:" + E.GetMessage ());
}
private void _HandleRecordComplete () {
// 结束录音
TV.Settext ("停止录音:" + temediaPath);
}
Void privado _HandleRecordVoice (int _db) {
// 声音事件侦听 , 转换成分贝
tvRecord.settext (""+_ db);
}
@Override
public boolean onCreateOptionsMenu (menu do menu) {
// infla o menu; Isso adiciona itens à barra de ação, se estiver presente.
getMenuinflater (). Infle (r.menu.main, menu);
retornar true;
}
}