您的位置:首页 > 移动开发 > 微信开发

Android 实现长按录音获取实时音量显示图片(类似微信)

2015-10-30 15:39 896 查看
本文主要是提供录音实时音量的显示(类似微信发语音的效果),对于录音的过程不做阐述(基本都会),有源码下载。

1. 创建一个Layout

<LinearLayout
android:layout_width="187dp"
android:layout_height="187dp"
android:background="@drawable/bg_sound"
android:gravity="center"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/record" />

<ImageView
android:id="@+id/iv_volume"
android:layout_width="41dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_gravity="bottom"
android:scaleType="fitStart"
android:src="@drawable/p1" />
</LinearLayout>
</LinearLayout>




2. 在activity_main中布局,将step 1 中的layout导入并隐藏

<ListView
android:id="@+id/lv_custom_bell"
android:layout_width="match_parent"
android:layout_height="430dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:choiceMode="singleChoice" >
</ListView>

<Button
android:id="@+id/bt_record"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/lv_custom_bell"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="10dp"
android:background="@drawable/bg_bt_login"
android:text="按住录音"
android:textColor="#ffffff" />

<LinearLayout
android:id="@+id/ll_record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:visibility="gone" >

<include
android:id="@+id/include1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/layout_custom_sound" />
</LinearLayout>


3.. 设置按钮的OnTouchListener事件

bt.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
bt.setText("松开结束");
mRecorduUtil.startRecord();
if (mRecorduUtil.isRecording()) {
//将step2中的linearlayout设为可视
ll_record.setVisibility(View.VISIBLE);
/**
*启动线程<注意!这里不能用handler.post启动Runnable,否则无法removeCallBack>
*/
Thread t = new Thread(mPollTask);
t.start();
}
break;

case MotionEvent.ACTION_UP:
bt.setText("按住录音");
ll_record.setVisibility(View.GONE);
mRecorduUtil.stopRecord();
updateBell();
mHandler.removeCallbacks(mPollTask);
break;
}
return true;
}
});


4.. 通过Handler定时获取音量更新图片

private Runnable mPollTask = new Runnable() {
public void run() {
int mVolume = mRecorduUtil.getVolume();
Log.d("volume", mVolume + "");
updateVolume(mVolume);
mHandler.postDelayed(mPollTask, 100);
}
};

// 更新列表
private void updateBell() {
List<Bell> list = new FileUtil().getCustomBell(RecordUtil.AUDIO_DIR);
lv.setAdapter(new CustomBellAdapter(list, MainActivity.this));
}

// 更新音量图
private void updateVolume(int volume) {
switch (volume) {
case 1:
iv_volume.setImageResource(R.drawable.p1);
break;
case 2:
iv_volume.setImageResource(R.drawable.p2);
break;
case 3:
iv_volume.setImageResource(R.drawable.p3);
break;
case 4:
iv_volume.setImageResource(R.drawable.p4);
break;
case 5:
iv_volume.setImageResource(R.drawable.p5);
break;
case 6:
iv_volume.setImageResource(R.drawable.p6);
break;
case 7:
iv_volume.setImageResource(R.drawable.p7);
break;
default:
break;
}
}


5.. 获取音量值

// 获取音量值,只是针对录音音量
public int getVolume() {
int volume = 0;
// 录音
if (mRecorder != null && recording) {
volume = mRecorder.getMaxAmplitude() / 650;
Log.d("db", volume + "");
if (volume != 0)
volume = (int) (10 * Math.log10(volume)) / 3;
Log.d("volume", volume + "");
}
return volume;
}


6.. 效果图



源码下载地址:http://download.csdn.net/detail/qq_25697993/9227123
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: