android 仿微信聊天界面 以及语音录制功能,Android仿微信录制语音功能

本文实例为大家分享了android仿微信录制语音的具体代码,供大家参考,具体内容如下

前言

我把录音分成了两部分

1.ui界面,弹窗读秒

2.一个类(包含开始、停止、创建文件名功能)

第一部分

由于6.0权限问题,点击按钮申请权限通过则弹窗,

弹窗布局popw_record.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

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">

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" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginleft="50dp"

android:layout_marginright="50dp"

android:gravity="center"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@mipmap/luyin" />

android:id="@+id/timer"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margintop="20dp"

android:format="%s" />

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"

/>

弹弹弹

/**

* 开始录音

*/

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";

}

}

这是从我代码中择出来的,加上权限应该是可以的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值