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 ...
随机推荐
- codeforces C. No to Palindromes!
http://codeforces.com/contest/465/problem/C 题意:给你一个字符串,然后按照字典序找出下一个字符串,这个字符串中不能含有长度大于等于2的子串为回文串,如果含有 ...
- Hosting Multiple Service Implementations On The Same Port With WCF
Hosting Multiple Service Implementations On The Same Port With WCF Recently I have been playing arou ...
- bzoj 1013 [JSOI2008]球形空间产生器sphere(高斯消元)
1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3584 Solved: 1863[Subm ...
- jQuery 数据 DOM 元素 核心 属性
jQuery 参考手册 - 数据 .clearQueue() 从序列中删除仍未运行的所有项目 .clearQueue(queueName) $("div").clearQueue( ...
- Bzoj 2818: Gcd 莫比乌斯,分块,欧拉函数,线性筛
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 3241 Solved: 1437[Submit][Status][Discuss ...
- Eclipse Maven插件无法搜索远程库
创建Maven工程,发现添加依赖“Add Dependency”的时候无法自动搜索远程库. 导致此问题的可能原因: 1.update index的时候失败了. 解决:打开Window/Show Vie ...
- Xmpp integration with Asterisk
http://gnu-linux.org/xmpp-integration-with-asterisk.html Xmpp stands for eXtensible Messaging and Pr ...
- mvn archetyoe:generate -DarchetypeCatalog=internal
可以使用 $mvn archetype:generate -DarchetypeCatalog=internal archetypeCatalog表示插件使用的archetype元数据,默认值为rem ...
- Django中的cookie与session
cookie与session的实现原理 HTTP被设计为”无状态”,每次请求都处于相同的空间中. 在一次请求和下一次请求之间没有任何状态保持,我们无法根据请求的任何方面(IP地址,用户代理等)来识别来 ...
- Android学习之 sildingmenu
仿SlidingMenu Android抽屉菜单效果drawer menu - appdoll.com Android "多方向"抽屉 - 开源中国社区 自定义Android滑动式 ...