温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • Android如何实现点击持续录音,松开结束录音并实现随着分贝的大小改变图片

Android如何实现点击持续录音,松开结束录音并实现随着分贝的大小改变图片

发布时间:2021-10-21 10:54:11 来源:亿速云 阅读:163 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关Android如何实现点击持续录音,松开结束录音并实现随着分贝的大小改变图片的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

显示录音大小的DIALOG实现

public class VioceDomio {
    private Context mContext;  
    private AlertDialog dialog ;
    private AlertDialog.Builder adialogbuile;  
    private ImageView vioce_show;

    public VioceDomio(Context mContext) {
        this.mContext=mContext;
    }

    public void showRecordingDialog() {
        adialogbuile = new AlertDialog.Builder(mContext, R.style.Theme_AudioDialog);  
        LayoutInflater inflater = LayoutInflater.from(mContext);  
        View view=inflater.inflate(R.layout.voice_chage,null);  
        vioce_show=(ImageView) view.findViewById(R.id.vioce_show);

        adialogbuile.setView(view);
        dialog = adialogbuile.create();

        dialog.show();
        dialog. getWindow().setLayout(500, 450);

    }

    public void HideRecordingDialog() {
        if(dialog!=null && dialog.isShowing()) {
            dialog.dismiss();
            dialog=null;
        }
    }

    public void ChangeRecordingDialog(double db) {    //根据录音的分贝改变大小
        if(db>=1 && db<=18) {
            vioce_show.setImageResource(R.drawable.voicesearch_feedback001);

        }else if(db>=19 && db<=37) {
            vioce_show.setImageResource(R.drawable.voicesearch_feedback002);
        }else if(db>=19 && db<=37) {
            vioce_show.setImageResource(R.drawable.voicesearch_feedback003);
        }
        else if(db>=38 && db<=56) {
            vioce_show.setImageResource(R.drawable.voicesearch_feedback004);
        }else if(db>=57 && db<=75) {
            vioce_show.setImageResource(R.drawable.voicesearch_feedback005);
        }else if(db>=76 && db<=94) {
            vioce_show.setImageResource(R.drawable.voicesearch_feedback006);
        }else if(db>=95 && db<=100) {
            vioce_show.setImageResource(R.drawable.voicesearch_feedback007);
        }
    }
}

Dialog加载的XML页面实现

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="90dp"  
    android:layout_marginTop="5dp"  

    android:id="@+id/bianhua">

    <ImageView
        android:id="@+id/vioce_show"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/voicesearch_feedback001" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="正在录音中" 
        android:gravity="center"
        />

</RelativeLayout>

主界面的试下

  1. activoti.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button
            android:id="@+id/speak"
            android:layout_width="wrap_content"
            android:layout_height="42dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:paddingLeft="10dp"
            android:layout_weight="0.62"
            android:background="@drawable/search_ba"
            android:drawableLeft="@drawable/speak_1"
             android:gravity="center"
            android:text="按住请讲话!!!"
            android:textSize="15sp" 
            android:singleLine="true"
            android:visibility="gone"
              />
    </LinearLayout >
  2. MainActiovity.java实现

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activoti);
        send=(Button) findViewById(R.id.send);
    
        viocedomio=new VioceDomio(this);
        speak.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                CountingThreadextends thend=new CountingThreadextends();
                if(event.getAction()==MotionEvent.ACTION_DOWN) {   //如果按钮处于按下状态
                    //录音
                    if(!Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
                        Toast.makeText(ChatActivity.this, "SD not ", Toast.LENGTH_SHORT).show();
                        return false;
                    }
                    String sound_path=time_path+"sound.amr";
                    try{
                        soundfile=new File(Environment.getExternalStorageDirectory().getCanonicalFile()+"/bishevoice/"+sound_path);
                        Log.d("send_filepath", Environment.getExternalStorageDirectory().getCanonicalFile()+"/bishevoice/"+sound_path);
                        mmedio=new MediaRecorder();
                        mmedio.setAudioSource(MediaRecorder.AudioSource.MIC);
                        mmedio.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        mmedio.setOutputFile(soundfile.getAbsolutePath());
                        mmedio.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        try{
                            //创建文件,准备录制
                            soundfile.createNewFile();
                            mmedio.prepare();
                        }catch(IllegalStateException  e) {
                            e.printStackTrace();
                        }
    
                        //开始录制
                        thend.start();
                        mmedio.start();
    
                        speak.setText("松开停止录音");
                        viocedomio.showRecordingDialog();   //显示录音的大小
    
                        final   Handler mHandler = new Handler() {  
                            @Override  
                                public void handleMessage(Message msg) { 
                                    viocedomio.ChangeRecordingDialog(Double.parseDouble(msg.obj.toString()));
                                }
                            };
    
                        new Thread(new Runnable() {   //启动线程根据改变录音显示大小
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                while(mmedio!=null) {
    
                                    double ratio = (double)mmedio.getMaxAmplitude() ;
                                    double db = 0;// 分贝
                                    if (ratio > 1)
                                        db = 20 * Math.log10(ratio);
                                   // Log.d(TAG,分贝值:+db);
                                   // mHandler.postDelayed(mUpdateMicStatusTimer, SPACE);   
                                    Message msg=new Message();
                                    msg.obj=db;
                                    mHandler.sendMessage(msg);
    
                                }
                            }
                        }).start();
    
                    }catch(Exception e) {
                        e.printStackTrace();
                    }
                }
    
                if(event.getAction()==MotionEvent.ACTION_UP) {   //如果是松开
                    if(soundfile != null && soundfile.exists()) {  //停止录音
                        try {
                            mmedio.setOnErrorListener(null);
                            mmedio.setOnInfoListener(null);
                            mmedio.setPreviewDisplay(null);
                            mmedio.stop();
                            thend.interrupt();
    
                            //timetask.cancel();
                        }catch(Exception e) {
                             Log.i("Exception", Log.getStackTraceString(e));
                        }
    
                        mmedio.release();
                        mmedio=null;
                        speak.setText("按住请讲话!!!");
                        String content="";
    
                        if(content.isEmpty()) {
                            viocedomio.HideRecordingDialog();  //隐藏录音标志
                            Msg msg=new Msg(content,Msg.TYPE_SENT,Msg.TYPE_SENT_VOICE,Integer.toString(time_int),file_path);
                            msg.setChattype(0);
                            msglist.add(msg);
                            adapter.notifyDataSetChanged(); //当有新消息时,刷新listview中的显示
    
                            msgListView.setSelection(msglist.size()-1); //将listview定位到最后一行
                            }
                        }
    
                    }
                return true;
            }
        });

感谢各位的阅读!关于“Android如何实现点击持续录音,松开结束录音并实现随着分贝的大小改变图片”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI