首先是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>

Android录音应用

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

// 获得控件public void get_con(){btn_talk = (Button)findViewById(R.id.btn_talk);btn_talk.setOnTouchListener(new OnTouchListener(){@Overridepublic 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文件可以正常播放。

更多相关文章

  1. 窗体两个按钮各占一半
  2. 自定义单选按钮(RadioButton)的样式
  3. Android按钮控件之RadioGroup和RadioButton
  4. android单选按钮RadioGroup的详细使用
  5. Android 正则表达式验证手机和邮箱格式是否正确
  6. Android Studio点击按钮更换背景图片
  7. Android中文合集(5)(126+8篇)(chm格式)
  8. Android 自定义圆角按钮

随机推荐

  1. android检测当前网络是否可用
  2. Android(安卓)bluetooth介绍(三): 蓝牙扫描(
  3. android recovery 模式启动进入流程
  4. 欢迎使用CSDN-markdown编辑器
  5. Android(安卓)Tools集合下载
  6. Android(安卓)中文 API (18) —— AbsSeekB
  7. android DPI与分辨率的关系及计算方式
  8. Android(安卓)NDK开发:SeetaFace2实现人脸
  9. 一步步教你为网站开发Android客户端
  10. android背景选择器selector用法汇总