Android MediaRecorder录音与播放
上一篇讲到了使用意图录音。这篇文章将使用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录音与播放的更多相关文章
- Android 自定义录音、播放动画View,让你的录音浪起来
最近公司项目有一个录音的录制和播放动画需求,然后时间是那么紧,那么赶紧开撸. 先看效果图 嗯,然后大致就是这样,按住录音,然后有一个倒计时,最外层一个进度条,还有一个类似模拟声波的动画效果(其实中间的 ...
- Android开发教程 录音和播放
首先要了解andriod开发中andriod多媒体框架包含了什么,它包含了获取和编码多种音频格式的支持,因此你几耍轻松把音频合并到你的应用中,若设备支持,使用MediaRecorder APIs便可以 ...
- Android平台下实现录音及播放录音功能的简介
录音及播放的方法如下: package com.example.audiorecord; import java.io.File; import java.io.IOException; import ...
- Android 实时录音和回放,边录音边播放 (KTV回音效果)
上一篇介绍了如何使用Mediarecorder来录音,以及播放录音.不过并没有达到我的目的,一边录音一边播放.今天就讲解一下如何一边录音一边播放.使用AndioRecord录音和使用AudioTrac ...
- Android 录音和播放
今天工作上需要做一个一边录音一边播放的功能,大致原因是有一个外部设备输入音频到我们机器,然后我们机器需要马上把音频播放出来.所以了解了一些有关录音和播放的知识.接到这个任务的第一反应就是看看Andro ...
- [Android] 录音与播放录音实现
http://blog.csdn.net/cxf7394373/article/details/8313980 android开发文档中有一个关于录音的类MediaRecord,一张图介绍了基本的流程 ...
- Android调用手机摄像头使用MediaRecorder录像并播放
最近在项目开发中需要调用系统的摄像头录像并播放. 在开发中遇到了两个问题,记录下: (1)开发过程中出现摄像头占用,启动失败,报错.但是我已经在onDestory()中关闭了资源. 报错原因:打开程序 ...
- MT6737 Android N 平台 Audio系统学习----录音到播放录音流程分析
http://blog.csdn.net/u014310046/article/details/54133688 本文将从主mic录音到播放流程来进行学习mtk audio系统架构. 在AudioF ...
- Android MediaRecorder实现暂停断点录音功能
基本原理如下:MediaRecorder通过MIC录音,系统没有自带的pause功能,每次暂停录音,都会结束本次的录音.现在本人的设计思路是:MediaRecorder录音暂停时,保存这段所录下的音频 ...
随机推荐
- DLL动态库多次加载问题
原因涉及DLL加载和运行机制,主要有两点:1)DLL动态链接库无法独立运行,必须由一个应用程序进程加载到进程空间后才能使用.加载DLL的进程称为宿主进程.被加载的DLL属于宿主进程,不属于宿主进程内某 ...
- css内容补充之其它
1.overflow 当图片大小,超出div的大小时,可以指定overflow值为auto(带滚动条).hidden(隐藏,只显示一块): hover 当鼠标移动到当前标签上时,以下css属性才生效:
- devops issue
1.Nginx(refercence:https://zhuanlan.zhihu.com/p/24382606) summary: DJANGO_PROJECT = /home/django/dja ...
- 「 HDOJ P3887 」 Counting Offspring
翻译 题目描述 给你一棵树,和它的树根 $P$,并且节点从 $1\rightarrow n$ 编号,现在定义 $f(i)$ 为 $i$ 的子树中,节点编号小于 $i$ 的节点的个数. 输入格式 有多组 ...
- MYSQL:WARN: Establishing SSL connection without server's identity verification is not recommended.
WARN: Establishing SSL connection without server's identity verification is not recommended. Accordi ...
- (3) openssl genrsa(生成rsa私钥)
genrsa用于生成RSA私钥,不会生成公钥,因为公钥提取自私钥,如果需要查看公钥或生成公钥,可以使用openssl rsa命令. 使用man genrsa查询其用法. openssl genrsa ...
- java 自动拆箱 自动装箱
自动装箱的定义就是 基本数据类型赋值给包装类型, 拆箱则相反. Integer integer = 122; // 自动装箱 int num = integer; //自动拆箱 想看一下源码是怎么 ...
- CSS3---渲染属性
1.计数器 CSS3计数器( CSS Counters )可以允许我们使用css对页面中的任意元素进行计数,实现类似于有序列表的功能.与有序列表相比,它的突出特性在于可以对任意元素计数,同时实现个性化 ...
- 使用github中py12306抢票系得
首先需要安装最新的python:安装步骤见:https://www.cnblogs.com/weven/p/7252917.html 其次下载python源码: 链接:https://pan.baid ...
- 【HIHOCODER 1478】 水陆距离(BFS)
描述 给定一个N x M的01矩阵,其中1表示陆地,0表示水域.对于每一个位置,求出它距离最近的水域的距离是多少. 矩阵中每个位置与它上下左右相邻的格子距离为1. 输入 第一行包含两个整数,N和M. ...