Android开发手记(31) 使用MediaRecorder录音
使用Android手机的时候,有时我们会用到录音功能,本文简单的介绍了如何使用MediaRecorder通过手机自带麦克风进行录音。
首先,既然是录音,我们需要录音和写外存的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
然后,我们创建一个录音的方法startRecord(),当我们单击录音按钮时调用这个方法来进行录音。录音的过程为:
(1)确定录音的文件的存放位置
(2)实例化一个MediaRecorder对象,并设置其参数
(3)调用MediaRecorder.prepare()准备录音
(4)调用MediaRecorder.start()开始录音
public void startRecord(){
if(mr == null){
File filePath = new File(Environment.getExternalStorageDirectory(), "myRecord");
File fileName = new File(filePath, System.currentTimeMillis() + ".amr");
try {
if (!filePath.exists()) {
filePath.mkdirs();
}
if (!fileName.exists()) {
fileName.createNewFile();
}
} catch(IOException e){
e.printStackTrace();
}
mr = new MediaRecorder();
mr.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置录音的输入源
mr.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB); // 设置输出格式
mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB); // 设置编码格式
mr.setOutputFile(fileName.getAbsolutePath()); // 设置输出文件名
try{
mr.prepare();
mr.start();
textView.setText("文件名:"+fileName.getAbsolutePath());
} catch(IOException e){
e.printStackTrace();
}
}
}
然后我们创建一个方法stopRecord()来停止录音,调用MediaRecorder.stop()可以停止录音,调用MediaRecorder.release()释放录音对象。然后将MediaRecorder指针置空以便下一次录音可以实例化新的MediaRecorder对象。
public void stopRecord(){
if(mr != null){
mr.stop();
mr.release();
mr = null;
}
}
最后,在MainActivity中为按钮添加单击事件,并调用上述方法即可实现录音。完整代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.doodle.myapplication"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/> </manifest>
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import java.io.File;
import java.io.IOException; public class MainActivity extends Activity { private Button button;
private TextView textView;
private boolean isStart = false;
private MediaRecorder mr = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isStart){
startRecord();
button.setText("停止录音");
isStart = false;
}
else {
stopRecord();
button.setText("开始录音");
isStart = true;
}
}
}); } public void startRecord(){
if(mr == null){
File filePath = new File(Environment.getExternalStorageDirectory(), "myRecord");
File fileName = new File(filePath, System.currentTimeMillis() + ".amr");
try {
if (!filePath.exists()) {
filePath.mkdirs();
}
if (!fileName.exists()) {
fileName.createNewFile();
}
} catch(IOException e){
e.printStackTrace();
} mr = new MediaRecorder();
mr.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置录音的输入源
mr.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB); // 设置输出格式
mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB); // 设置编码格式
mr.setOutputFile(fileName.getAbsolutePath()); // 设置输出文件名 try{
mr.prepare();
mr.start();
textView.setText("文件名:"+fileName.getAbsolutePath());
} catch(IOException e){
e.printStackTrace();
}
}
} public void stopRecord(){
if(mr != null){
mr.stop();
mr.release();
mr = null;
}
} }
Android开发手记(31) 使用MediaRecorder录音的更多相关文章
- Android 开发手记一NDK编程实例
在Android上,应用程序的开发,大部分基于Java语言来实现.要使用c或是c++的程序或库,就需要使用NDK来实现.NDK是Native Development Kit的简称.它是一个工具集,集成 ...
- Android开发--仿微信语音对讲录音
原文地址:http://www.2cto.com/kf/201502/378704.html 自微信出现以来取得了很好的成绩,语音对讲的实现更加方便了人与人之间的交流.今天来实践一下微信的语音对讲的录 ...
- Android开发手记(28) Handler和Looper
Android的消息处理有三个核心类:Looper,Handler和Message.其实还有一个Message Queue(消息队列),但是MQ被封装到Looper里面了,我们不会直接与MQ打交道.平 ...
- Android开发手记(24) Log的使用及颜色的更改
在程序开发过程中,LOG是广泛使用的用来记录程序执行过程的机制,它既可以用于程序调试,也可以用于产品运营中的事件记录.在Android系统中,提供了简单.便利的LOG机制,开发人员可以方便地使用.本文 ...
- Android开发手记(22) 传感器的使用
Android的传感器主要包括八大传感器,他们分别是:加速度传感器(accelerometer).陀螺仪(gyroscope).方向传感器(orientation).磁力传感器(magnetic fi ...
- Android开发手记(18) 数据存储三 SQLite存储数据
Android为数据存储提供了五种方式: 1.SharedPreferences 2.文件存储 3.SQLite数据库 4.ContentProvider 5.网络存储 SQLite 是以嵌入式为目的 ...
- Android开发手记(17) 数据存储二 文件存储数据
Android为数据存储提供了五种方式: 1.SharedPreferences 2.文件存储 3.SQLite数据库 4.ContentProvider 5.网络存储 本文主要介绍如何使用文件来存储 ...
- Android开发手记(32) 使用摄像头拍照
在Android中,使用摄像头拍照一般有两种方法, 一种是调用系统自带的Camera,另一种是自己写一个摄像的界面. 我们要添加如下权限: <uses-permission android:na ...
- Android开发手记(30) 触摸及手势操作
触摸操作在现在智能手机系统中起到举足轻重的作用,本文将对安卓中的触摸以及一些简单手势的操作进行简单的介绍. 1.触摸 首先是关于触摸的判断,有两种方法可以判断的触摸操作. (1)setOnTouchL ...
随机推荐
- 【Tools】Apache Maven 入门篇 ( 上 )
作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法.这个入门篇分上下两篇.本文着重动手,用 mave ...
- 【转】Open XML SDK class structure
Open XML SDK class structure March 27, 2012 by Vincent I’ve gotten a few questions on the class stru ...
- Life Forms
poj3294:http://poj.org/problem?id=3294 题意:就是求n个串的中一个最大的子串,这个子串在超过n/2的串中出现. 题解:这是一道好题.首先一种解法就是用后缀数组来搞 ...
- Windows窗口样式速查参考,Delphi窗口控件的风格都有它们来决定(附Delphi何时用到它们,并举例说明)good
/* 窗口样式参考列表(都是GetWindowLong的GWL_STYLE风格,都是TCreateParams.Sytle的一部分),详细列表如下:https://msdn.microsoft.com ...
- Delphi中用Webbrowser加载百度地图滚轮失效(ApplicationEvents里使用IsChild提前判断是哪个控件的消息)
在Delphi中使用Webbrowser加载百度地图时,点击了其它界面,再回到百度地图中,即使点击了鼠标,再用滚轮也不能缩 放地图,除非点地图里面的自带的控件,之后才能缩放,原因是因为其它窗体控件获得 ...
- Apache log4net™ Config Examples
Overview This document presents example configurations for the built-in appenders. These configurati ...
- ♫【Git】
git - 简易指南 (√) fatal:remote error:You can't push to git://github.com/username/*.git (√) git clone g ...
- VM Depot 镜像新增系列II – 学习管理系统,内容管理系统以及平台管理工具
发布于 2014-06-23 作者 刘 天栋 继上周企业管理软件和电子商务镜像的加盟之后,我们看到又有一批内容管理解决方案(CMS),学习管理解决方案(LMS)以及平台管理工具 (如 Open ...
- 【转】蓝牙物理链路类型:SCO和ACL链路
原文网址:http://blog.chinaunix.net/uid-23193900-id-3272233.html 蓝牙物理链路ACL(Asynchronous Connectionless), ...
- 窥探Unity5渲染内部之解析UnityShaderVariables.cginc
unity5的UnityShaderVariables.cginc比unity4大了1kb这里装着unity shader 大部分内部参数,写这个方便以后自己查询 Camera参数 uniform f ...