Android仿微信录制语音功能
作者:L代码 发布时间:2022-10-18 15:54:18
标签:Android,微信,录制语音
本文实例为大家分享了Android仿微信录制语音的具体代码,供大家参考,具体内容如下
前言
我把录音分成了两部分
1.UI界面,弹窗读秒
2.一个类(包含开始、停止、创建文件名功能)
第一部分
由于6.0权限问题,点击按钮申请权限通过则弹窗,如何申请权限
弹窗布局popw_record.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="260dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@drawable/take_phone"
android:orientation="vertical">
<ImageView
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="10dp"
android:src="@mipmap/guanbi" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/luyin" />
<Chronometer
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:format="%s" />
<TextView
android:id="@+id/startRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/playrecord"
android:layout_marginTop="20dp"
android:background="@color/background"
android:padding="10dp"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
弹弹弹
/**
* 开始录音
*/
private void showPopup() {
final View contentView = LayoutInflater.from(Orderdeatil.this).inflate(R.layout.popw_record, null);
mPopWindow = new PopupWindow(contentView, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
mPopWindow.setContentView(contentView);
TextView startRe = (TextView) contentView.findViewById(R.id.startRecord);
startRe.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP://松开事件发生后执行代码的区域
if (mPopWindow != null) {
mPopWindow.dismiss();
sr.stopRecording();
}
break;
case MotionEvent.ACTION_DOWN://按住事件发生后执行代码的区域
Chronometer timer = (Chronometer) contentView.findViewById(R.id.timer);
timer.setBase(SystemClock.elapsedRealtime());//计时器清零
timer.start();//开始录音的提示
sr.startRecording();
break;
case MotionEvent.ACTION_CANCEL:
if (mPopWindow != null) {
mPopWindow.dismiss();
sr.stopRecording();//停止录音
}
break;
default:
break;
}
return true;
}
});
ImageView close = (ImageView) contentView.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPopWindow.dismiss();
}
});
mPopWindow.setTouchable(true);
mPopWindow.setFocusable(true);
mPopWindow.setBackgroundDrawable(new BitmapDrawable());
mPopWindow.setOutsideTouchable(true);
mPopWindow.setTouchInterceptor(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
mPopWindow.dismiss();
return true;
}
return false;
}
});
View rootview = LayoutInflater.from(Orderdeatil.this).inflate(R.layout.activity_orderdeatil, null);
mPopWindow.showAtLocation(rootview, Gravity.CENTER, 0, 0);
}
第二部分 工具类
class SoundRecorder {
public void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mRecorder.setOutputFile(newFileName());
try {
// 准备好开始录音
mRecorder.prepare();
mRecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stopRecording() {
if (mRecorder != null) {
//added by ouyang start
try {
//下面三个参数必须加,不加的话会奔溃,在mediarecorder.stop();
//报错为:RuntimeException:stop failed
mRecorder.setOnErrorListener(null);
mRecorder.setOnInfoListener(null);
mRecorder.setPreviewDisplay(null);
mRecorder.stop();
} catch (IllegalStateException e) {
// TODO: handle exception
Log.i("Exception", Log.getStackTraceString(e));
} catch (RuntimeException e) {
// TODO: handle exception
Log.i("Exception", Log.getStackTraceString(e));
} catch (Exception e) {
// TODO: handle exception
Log.i("Exception", Log.getStackTraceString(e));
}
//added by ouyang end
mRecorder.release();
mRecorder = null;
upRecord();
}
}
public String newFileName() {
mFileName = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String s = new SimpleDateFormat("yyyy-MM-dd hhmmss")
.format(new Date());
return mFileName += "/rcd_" + s + ".mp3";
}
}
这是从我代码中择出来的,加上权限应该是可以的。
来源:https://blog.csdn.net/qq_34882418/article/details/81346299


猜你喜欢
- 一、什么是单例模式?单例设计模式(Singleton Design Pattern)理解起来非常简单。一个类只允许创建一个对象(或者实例),
- 本文实例讲述了C#实现计算一个点围绕另一个点旋转指定弧度后坐标值的方法。分享给大家供大家参考。具体如下:1.示例图P(x1,y1)以点A(a
- Spring中实现多线程,其实非常简单,只需要在配置类中添加@EnableAsync就可以使用多线程。在希望执行的并发方法中使用@Async
- Command模式是最让我疑惑的一个模式,我在阅读了很多代码后,才感觉隐约掌握其大概原理,我认为理解设计模式最主要是掌握起原理构造,这样才对
- 1.首先是编辑器的乱码,这个很好解决,file->settings->appearence里面有个Name设置成支持中文的字 体
- 在用java的io流读写文件时,总是被它的各种流能得很混乱,有40多个类,理清啦,过一段时间又混乱啦,决定整理一下!以防再忘Java输入/输
- 本文实例讲述了C#编程读取文档Doc、Docx及Pdf内容的方法。分享给大家供大家参考。具体分析如下:Doc文档:Microsoft Wor
- 引言这一篇文章我们就通过介绍滑动冲突的规则和一个实例来更加深入的学习View的事件分发机制。1、外部滑动方向和内部滑动方向不一致考虑这样一种
- 一、文件上传原理 1、文件上传的前提:a、form表单的method必须是postb、form表单的enctype必须是multi
- C#史上最简单读写xml文件方式,创建控制台应用程序赋值代码,就可以运行,需要改动,请自行调整using System;using Syst
- Elastic Search是一个开源的,分布式,实时搜索和分析引擎。Spring Boot为Elasticsearch及Spring Da
- 本文实例讲述了C#判断系统是32位还是64位的方法。分享给大家供大家参考。具体如下:public static int GetOSBit()
- 多线程可以说是面试官最喜欢拿来问的题目之一了,可谓是老生之常谈,不管你是新手还是老司机,我相信你一定会在面试过程中遇到过有关多线程的一些问题
- 一、C#正则表达式符号模式字符描述\转义字符,将一个具有特殊功能的字符转义为一个普通字符,或反过来^匹配输入字符串的开始位置$匹配输入字符串
- java 8的新特性之一就是lambda表达式,parallelStream()都说性能会比较高,现一探究竟。话不多说,上代码: @Test
- 语法糖(Syntactic sugar)是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中
- 本文实例为大家分享了Java使用单链表实现约瑟夫环的具体代码,供大家参考,具体内容如下构建一个单向的环形链表思路1.先创建第一个节点, 让f
- 拖曳小球WPF的拖曳效果,基本配置一下,就可以了,但是自绘的话,就得自己控制,按键点击,按键移动和按键松开的事件,与其配合达到目的。这个效果
- [LeetCode] 5. Longest Palindromic Substring 最长回文子串Given a string
- 快速排序类using System;using System.Data;using System.Config