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

本文主要是提供录音实时音量的显示(类似微信发语音的效果),对于录音的过程不做阐述(基本都会),有源码下载。
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

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值