欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android录音应用实例教程

时间:2021-04-26 11:02:06|栏目:Android代码|点击:

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

首先是xml布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://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>

运行效果如下图所示:

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程序设计有所帮助。

上一篇:Studio 编译报错:compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.的解决办法

栏    目:Android代码

下一篇:Android Studio获取SHA1值实例详解

本文标题:Android录音应用实例教程

本文地址:http://www.codeinn.net/misctech/108770.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有