上一篇讲到了使用意图录音。这篇文章将使用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. Visual Studio 2017部署 webStrom开发的nodejs项目

    vs点击文件--新建--项目--JavaScript--Node.js--通过现有Node.js代码 wxxcx为nodejs项目根目录,然后右击整个项目--属性:1.启动目录2.默认打开的链接3.设 ...

  2. CentOS7.6 静态IP配置

    1:选中安装好的虚拟机,点击“编辑”,然后选择“虚拟网络编辑器(N…)”,如下图所示: 2:选择桥接模式,在桥接到指定的本地网卡即可."确定"保存 3:选中虚拟机,右击虚拟机,选择 ...

  3. 洛谷 P2858 奶牛零食

    https://www.luogu.org/problemnew/show/P2858 毫无疑问区间dp. ![区间dp入门] 我们定义dp[i][j]表示从i到j的最大收益,显然我们需要利用比较小的 ...

  4. oracle函数笔记(1)

    1.在数据库中显示当前时间:函数(sysdate) select sysdate from dual: 2.数据库中显示 :年/月/日 时/分/秒 ---函数:to_char(字段,'yyyy-mm- ...

  5. (十八)python 3 回调函数

    回调函数:把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的 ...

  6. day22 01 初识面向对象----简单的人狗大战小游戏

    day22 01 初识面向对象----简单的人狗大战小游戏 假设有一个简单的小游戏:人狗大战   怎样用代码去实现呢? 首先得有任何狗这两个角色,并且每个角色都有他们自己的一些属性,比如任务名字nam ...

  7. python基础——8(装饰器)

    一.nonlocal关键字 def outer(): num = 0 def inner(): # 如果想在被嵌套的函数中修改外部函数变量(名字)的值 nonlocal num # 将 L 与 E(E ...

  8. SpringCloud源码地址

    SpringCloud实战源代码 https://github.com/springcloud/spring-cloud-code.git

  9. IntrospectorCleanupListener监听器防止内存溢出

    <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</ ...

  10. 大数据学习——linux常用命令(一)

    一.基本日常操作命令 1 查看当前所在工作目录的全路径 pwd 2 查看当前系统的时间 date 设置时间,date -s"2018-11-12" 修改时间后,需要写入硬件bios ...