Android仿微信发送语音消息的功能及示例代码
作者:try_zp_catch 时间:2021-07-06 19:31:39
微信的发送语音是有一个向上取消的,我们使用onTouchListener
来监听手势,然后做出相应的操作就行了。
直接上代码:
//语音操作对象
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;
}
然后在清单文件中加上需要的权限就行了:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
也可以在按下之后播放动画,显示正在录音,然后在抬起动作之后结束动画,这样就比较形象了。
总结
以上所述是小编给大家介绍的Android仿微信发送语音消息的功能及示例代码网站的支持!
来源:http://blog.csdn.net/try_zp_catch/article/details/77600304
标签:android,微信,语音消息
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现截取PDF指定页并进行图片格式转换功能
2023-08-24 02:58:56
WinForm生成验证码图片的方法
2022-05-11 17:49:33
利用maven引入第三方jar包以及打包
2023-11-15 04:23:17
![](https://img.aspxhome.com/file/2023/0/59010_0s.jpg)
springcloud中RabbitMQ死信队列与延迟交换机实现方法
2023-05-04 04:25:54
Android实现文字上下滚动效果
2023-02-02 07:40:00
java servlet 几种页面跳转的方法
2023-08-02 18:12:07
JAVA的反射机制你了解多少
2023-11-29 16:46:38
![](https://img.aspxhome.com/file/2023/9/60609_0s.jpg)
一篇看懂Java中的Unsafe类
2022-02-13 04:02:38
Java 中的弱引用是什么
2021-08-21 08:10:46
详解Java实现缓存(LRU,FIFO)
2022-04-24 13:35:26
![](https://img.aspxhome.com/file/2023/1/64101_0s.png)
Springboot Vue实现单点登陆功能示例详解
2023-11-05 00:29:11
![](https://img.aspxhome.com/file/2023/7/58877_0s.png)
Android 基于IntentService的文件下载的示例代码
2023-10-28 23:26:02
Kotlin挂起函数原理示例剖析
2023-11-10 21:52:13
![](https://img.aspxhome.com/file/2023/0/113330_0s.jpg)
C#语言中字符类char的使用方法(总结)
2022-09-04 17:25:43
Spring Boot 集成Redisson实现分布式锁详细案例
2023-06-10 05:09:17
![](https://img.aspxhome.com/file/2023/5/83485_0s.png)
Android中实现TCP和UDP传输实例
2021-11-08 14:19:59
![](https://img.aspxhome.com/file/2023/4/114294_0s.png)
Java 实现网络爬虫框架详细代码
2021-12-11 05:15:43
Android 破解视频App去除广告功能详解及解决办法总结
2022-07-16 23:28:50
![](https://img.aspxhome.com/file/2023/8/108288_0s.png)
配置Ant执行Jmeter脚本过程详解
2023-11-09 22:35:29
![](https://img.aspxhome.com/file/2023/5/65645_0s.png)
Java设计模式之模板方法模式详解
2021-08-04 04:32:51
![](https://img.aspxhome.com/file/2023/2/70292_0s.png)