上一篇讲到了使用意图录音。这篇文章将使用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...");
} }

源码下载:

点击下载源代码

最新文章

  1. 返回值优化(RVO)
  2. python数字图像处理(13):基本形态学滤波
  3. Android 查看內存使用
  4. CSAPP2e:Shell lab 解答
  5. Linux无线网络设置(wpa_supplicant的使用)
  6. NOI2003 文本编辑器
  7. debian linux 中如何查看软件包是否已经安装和如何安装、卸载软件
  8. Log4j MDC Tomcat下报异常org.apache.log4j.helpers.ThreadLocalMap
  9. VS2008编译boost库
  10. ZTESoft 持续集成 编年史 之 持续集成建设---自主研发(总括)
  11. windows下如何创建没有名字的.htaccess文件
  12. 数据挖掘---Matplotib的学习
  13. cygwin 安装包管理器 apt-cyg
  14. 使用pyenv在系统中安装多个版本的python
  15. MySQL 并发测试中,线程数和数据库连接池的实验
  16. hive的jdbc使用
  17. Java中的面向对象II
  18. mysql 存储过程动态拼接sql并执行赋值
  19. hdu 5459(2015沈阳网赛) Jesus Is Here
  20. TStream实现多表提交

热门文章

  1. QT+创建两个不相干的窗口实现一个显示一个不显示
  2. sleep 和wait的差别
  3. vue 父子组件的加载顺序
  4. Linux中的FTP服务
  5. 上海苹果维修点分享苹果电脑MACBOOK故障维修常见案例
  6. mysql多字段组合删除重复行
  7. running Fluent on Apocrita Cluster
  8. Centos 7安装Mysql5.7
  9. python基础——5(元组、字典、集合)
  10. luogu3157 [CQOI2011]动态逆序对