怎么在Android应用中实现一个语音消息发送功能
发布时间:2021-01-04 16:44:19
来源:亿速云
阅读:152
作者:Leah
本篇文章为大家展示了怎么在Android应用中实现一个语音消息发送功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
直接上代码://语音操作对象
private MediaPlayer mPlayer = null;
private MediaRecorder mRecorder = null;
//语音文件保存路径
private String FileName = null;
FileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/soundrecord/amrsend.amr";
定义一个ImageView,然后给它设置监听事件:@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
beginX = motionEvent.getX();
beginY = motionEvent.getY();
Log.e("MainActivity", "beginX:" + beginX);
Log.e("MainActivity", "beginY:" + beginY);
Log.e("MainActivity", "ACTION_DOWN");
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(FileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e("MainActivity", "prepare() failed" + e);
}
mRecorder.start();
break;
case MotionEvent.ACTION_UP:
finalX = motionEvent.getX();
finalY = motionEvent.getY();
Log.e("MainActivity", "finalX:" + finalX);
Log.e("MainActivity", "finalY:" + finalY);
Log.e("MainActivity", "ACTION_UP");
mRecorder.stop();
mRecorder.release();
mRecorder = null;
if ((beginY - finalY) > 50) {
Log.e("MainActivity", "执行删除");
new File(FileName).delete();
Log.e("MainActivity", "执行删除后文件是否还存在:" + new File(FileName).exists());
}
break;
default:
break;
}
return true;
}
然后在清单文件中加上需要的权限就行了:
上述内容就是怎么在Android应用中实现一个语音消息发送功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。