上一篇讲到了使用意图录音。这篇文章将使用MediaRecorder类来录音,从而提供很多其它的灵活性。

效果图:

源码奉上:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/statusTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Status"
android:gravity="center_horizontal" /> <Button
android:id="@+id/button_startRecording"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開始录音" /> <Button
android:id="@+id/button_stopRecording"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止录音" /> <Button
android:id="@+id/button_playRecording"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放录音" /> <Button
android:id="@+id/button_finish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="完毕" /> </LinearLayout>

package com.multimediademo9mediarecorder;

import java.io.File;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener,
OnCompletionListener {
private TextView statusTextView;
private Button button_startRecording, button_stopRecording,
button_playRecording, button_finish;
private MediaRecorder recorder;
private MediaPlayer player;
private File audioFile; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); init();
} /**
* 实例化组件
*/
private void init() {
statusTextView = (TextView) findViewById(R.id.statusTextView);
// 当执行Activity时。将statusTextView的文本设置为“Ready”。
statusTextView.setText("Ready"); button_startRecording = (Button) findViewById(R.id.button_startRecording);
button_playRecording = (Button) findViewById(R.id.button_playRecording);
button_stopRecording = (Button) findViewById(R.id.button_stopRecording);
button_finish = (Button) findViewById(R.id.button_finish); button_startRecording.setOnClickListener(this);
button_playRecording.setOnClickListener(this);
button_stopRecording.setOnClickListener(this);
button_finish.setOnClickListener(this); button_playRecording.setEnabled(false);
button_stopRecording.setEnabled(false); player = new MediaPlayer();
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_startRecording:
try {
/**
* 当点击開始录音按钮时,将构造一个新的MediaRecorder,并调用setAudioSource、
* setOutputFormat和setAudioEncoder方法。
*/
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
/**
* 然后在SD卡上创建一个新的File对象,并调用MediaRecorder对象上的setOutputFile方法。 */
File path = new File(Environment.getExternalStorageDirectory()
.getAbsoluteFile() + "/files/");
path.mkdir();
audioFile = File.createTempFile("recording", ".3gp", path);
recorder.setOutputFile(audioFile.getAbsolutePath());
/**
* 调用MediaRecorder上的prepare方法。并開始录制。
*/
recorder.prepare();
recorder.start();
/**
* 最后更新statusTextView,而且更改那些按钮会被启用或禁用。
*/
statusTextView.setText("Recording");
button_playRecording.setEnabled(false);
button_stopRecording.setEnabled(true);
button_startRecording.setEnabled(false);
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.button_playRecording:
/**
* 播放录音,使用MediaPlayer构造的对象player
*/
player.start();
statusTextView.setText("playing");
button_playRecording.setEnabled(false);
button_stopRecording.setEnabled(false);
button_startRecording.setEnabled(false);
break;
case R.id.button_stopRecording:
/**
* 停止录制。并释放MediaRecorder对象。 */
try {
recorder.stop();
recorder.release(); player.setOnCompletionListener(this);
player.setDataSource(audioFile.getAbsolutePath());
player.prepare();
statusTextView.setText("Ready to Play!!");
button_playRecording.setEnabled(true);
button_stopRecording.setEnabled(false);
button_stopRecording.setEnabled(false);
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.button_finish:
finish();
break;
default:
break;
}
} @Override
public void onCompletion(MediaPlayer mp) {
button_playRecording.setEnabled(true);
button_startRecording.setEnabled(true);
button_stopRecording.setEnabled(false);
statusTextView.setText("Ready...");
} }

源码下载:

点击下载源代码

Android MediaRecorder录音与播放的更多相关文章

  1. Android 自定义录音、播放动画View,让你的录音浪起来

    最近公司项目有一个录音的录制和播放动画需求,然后时间是那么紧,那么赶紧开撸. 先看效果图 嗯,然后大致就是这样,按住录音,然后有一个倒计时,最外层一个进度条,还有一个类似模拟声波的动画效果(其实中间的 ...

  2. Android开发教程 录音和播放

    首先要了解andriod开发中andriod多媒体框架包含了什么,它包含了获取和编码多种音频格式的支持,因此你几耍轻松把音频合并到你的应用中,若设备支持,使用MediaRecorder APIs便可以 ...

  3. Android平台下实现录音及播放录音功能的简介

    录音及播放的方法如下: package com.example.audiorecord; import java.io.File; import java.io.IOException; import ...

  4. Android 实时录音和回放,边录音边播放 (KTV回音效果)

    上一篇介绍了如何使用Mediarecorder来录音,以及播放录音.不过并没有达到我的目的,一边录音一边播放.今天就讲解一下如何一边录音一边播放.使用AndioRecord录音和使用AudioTrac ...

  5. Android 录音和播放

    今天工作上需要做一个一边录音一边播放的功能,大致原因是有一个外部设备输入音频到我们机器,然后我们机器需要马上把音频播放出来.所以了解了一些有关录音和播放的知识.接到这个任务的第一反应就是看看Andro ...

  6. [Android] 录音与播放录音实现

    http://blog.csdn.net/cxf7394373/article/details/8313980 android开发文档中有一个关于录音的类MediaRecord,一张图介绍了基本的流程 ...

  7. Android调用手机摄像头使用MediaRecorder录像并播放

    最近在项目开发中需要调用系统的摄像头录像并播放. 在开发中遇到了两个问题,记录下: (1)开发过程中出现摄像头占用,启动失败,报错.但是我已经在onDestory()中关闭了资源. 报错原因:打开程序 ...

  8. MT6737 Android N 平台 Audio系统学习----录音到播放录音流程分析

    http://blog.csdn.net/u014310046/article/details/54133688 本文将从主mic录音到播放流程来进行学习mtk audio系统架构.  在AudioF ...

  9. Android MediaRecorder实现暂停断点录音功能

    基本原理如下:MediaRecorder通过MIC录音,系统没有自带的pause功能,每次暂停录音,都会结束本次的录音.现在本人的设计思路是:MediaRecorder录音暂停时,保存这段所录下的音频 ...

随机推荐

  1. 最短路 || POJ 1511 Invitation Cards

    已知图中从一点到另一点的距离,从1号点到另一点再从这一点返回1号点,求去到所有点的距离之和最小值 *解法:正着反着分别建图,把到每个点的距离加起来 spfa跑完之后dist数组就是从起点到每一点的最短 ...

  2. ubuntu14.04 RockMongo的配置

    安装php5 安装php5     sudo apt-get install php5 让Apache支持php      sudo apt-get install libapache2-mod-ph ...

  3. 条款24:若所有参数皆需要类型转换,请为此采用non-member函数(Declare non-member functions when type conversions should apply to all parameters)

    NOTE: 1.如果你需要为某个函数的所有参数(包括this指针所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member.

  4. Windows Server定时执行bat

    大家应该知道是在window服务器下使用bat批处理脚本文件,如果是Linux操作系统则是使用xshell脚本文件.由于自己是在做项目的时候对于文件系统中的日志进行定期删除对bat和xshell进行简 ...

  5. RabbitMQ 关键词解释

    源地址: https://www.cnblogs.com/hz04022016/p/6518138.html RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQ ...

  6. solr 时区问题

    本人使用solr版本5.0.0,使用jetty启动 solr默认UTC时区,与我们相差八小时,按照网络上资料修改 C:\Users\hp\Desktop\solr-5.0.0\bin 下的solr.i ...

  7. 杭电 1596 find the safest road (最小路径变形求最大安全度)

    Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条从u 到 v 的 ...

  8. 【POJ 2891】Strange Way to Express Integers(一元线性同余方程组求解)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  9. JVM——内存管理和垃圾回收

    1.  何为GC 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/51892567 Java与C语言相比的一个优势是,可以通过自己的JV ...

  10. 集合框架学习之List接口

    Java语言的java.util包中提供了一些集合类,这些集合类又被称为容器.用来完善数组的不足之处.集合类与数组的不同之处是,数组的长度是固定的,集合的长度是可变的:数组用来存放基本类型的数据,集合 ...