android开发分享Android录音应用实例教程

本文以实例形式较为详细的展示了android录音的实现方法,分享给大家供大家参考之用。具体方法如下: 首先是xml布局文件: <linearlayout

android开发分享Android录音应用实例教程以实例形式较为详细的展示了android录音的实现方法,分享给大家供大家参考之用。具体方法如下:

上述就是android开发分享Android录音应用实例教程的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

首先是xml布局文件:

  <linearlayout xmlns:android="https://schemas.android.com/apk/res/android"    xmlns:tools="https://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_gravity="center"    android:gravity="center"    android:orientation="vertical"    android:paddingbottom="@dimen/activity_vertical_margin"    android:paddingleft="@dimen/activity_horizontal_margin"    android:paddingright="@dimen/activity_horizontal_margin"    android:paddingtop="@dimen/activity_vertical_margin"    tools:context=".mainactivity" >       <button      android:id="@+id/btn_talk"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_gravity="center"      android:enabled="false"      android:text="talk"      android:textsize="30dp"      android:textstyle="bold" />     </linearlayout>    

运行效果如下图所示:

Android录音应用实例教程

mainactivity中定义按钮的点击监听器,按下按钮时开始录音,松开按钮时停止录音,类似于微信的操作方法。

  // 获得控件  public void get_con(){         btn_talk = (button)findviewbyid(r.id.btn_talk);    btn_talk.setontouchlistener(new ontouchlistener(){      @override      public boolean ontouch(view v, motionevent e) {        if (e.getaction() == motionevent.action_down){          // 开始录音          start_record();        }        else if (e.getaction() == motionevent.action_up){          // 停止录音          stop_record();        }        return false;      }    });  }    

开始录音的方法,使用了android.media.mediarecorder录音。首先判断sd卡是否存在,如果存在根据当前时间给创建一个录音文件,保存到预定的目录中,用mediarecorder类开始录音。

  // 开始录音  public void start_record(){    if (!environment.getexternalstoragestate().equals(android.os.environment.media_mounted)){           show_status("sd卡不存在,请插入sd卡!");         }    else{      try      {        // 获取当前时间        cur_date = new date(system.currenttimemillis());        str_file = formatter.format(cur_date);         // 创建保存录音的音频文件        send_sound_file = new file(environment.getexternalstoragedirectory().getcanonicalfile() + "/talk/send");        // 如果目录不存在        if (!send_sound_file.exists()){          send_sound_file.mkdirs();        }        send_sound_file = new file(environment.getexternalstoragedirectory().getcanonicalfile() + "/talk/send/" + str_file + ".amr");        recorder = new mediarecorder();        // 设置录音的声音来源        recorder.setaudiosource(mediarecorder.audiosource.mic);        // 设置录制的声音的输出格式(必须在设置声音编码格式之前设置)        recorder.setoutputformat(mediarecorder.outputformat.three_gpp);        // 设置声音编码的格式        recorder.setaudioencoder(mediarecorder.audioencoder.amr_nb);        recorder.setoutputfile(send_sound_file.getabsolutepath());        recorder.prepare();        // 开始录音        recorder.start();      }      catch (exception e)      {        show_status(e.tostring());      }    }  }    

停止录音的方法,相对简单。

  // 停止录音  public void stop_record(){    if (send_sound_file != null && send_sound_file.exists())    {      ses_id = ses_id + 1;      // 停止录音      recorder.stop();      // 释放资源      recorder.release();      recorder = null;    }    super.ondestroy();  }    

经过测试,录制的3gp文件可以正常播放。

希望android开发分享Android录音应用实例教程所述对大家的android程序设计有所帮助。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/940480.html

(0)
上一篇 2021年11月13日
下一篇 2021年11月13日

精彩推荐