录音及播放的方法如下:

package com.example.audiorecord;

import java.io.File;
import java.io.IOException; import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class RecordActivity extends Activity { private static final String LOG_TAG = "AudioRecordTest";
// 语音文件保存路径
private String FileName = null; // 界面控件
private Button startRecord;
private Button startPlay;
private Button stopRecord;
private Button stopPlay; // 语音操作对象
private MediaPlayer mPlayer = null;
private MediaRecorder mRecorder = null; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // 开始录音
startRecord = (Button) findViewById(R.id.startRecord);
startRecord.setText(R.string.startRecord);
// 绑定监听器
startRecord.setOnClickListener(new startRecordListener()); // 结束录音
stopRecord = (Button) findViewById(R.id.stopRecord);
stopRecord.setText(R.string.stopRecord);
stopRecord.setOnClickListener(new stopRecordListener()); // 开始播放
startPlay = (Button) findViewById(R.id.startPlay);
startPlay.setText(R.string.startPlay);
// 绑定监听器
startPlay.setOnClickListener(new startPlayListener()); // 结束播放
stopPlay = (Button) findViewById(R.id.stopPlay);
stopPlay.setText(R.string.stopPlay);
stopPlay.setOnClickListener(new stopPlayListener()); // 设置sdcard的路径
FileName = Environment.getExternalStorageDirectory().getAbsolutePath();
FileName = FileName + File.separator + "audiorecordtest.arm";
} // 开始录音
class startRecordListener implements OnClickListener { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setOutputFile(FileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
} } // 停止录音
class stopRecordListener implements OnClickListener { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
mRecorder.stop();
mRecorder.release();
mRecorder = null;
} } // 播放录音
class startPlayListener implements OnClickListener { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(FileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "播放失败");
}
} } // 停止播放录音
class stopPlayListener implements OnClickListener { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPlayer.release();
mPlayer = null;
} }
}

界面布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" /> <Button
android:id="@+id/startRecord"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/stopRecord"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/startPlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/stopPlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> </LinearLayout>

完整项目代码: http://download.csdn.net/detail/abc13939746593/6920293

Android平台下实现录音及播放录音功能的简介的更多相关文章

  1. 【转】Android平台下利用zxing实现二维码开发

    http://www.cnblogs.com/dolphin0520/p/3355728.html 现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研究了下二维码开发的东西,开源的二维码扫描 ...

  2. 【转】Android 平台下使用 i2c-tools

    原文网址:http://my.oschina.net/luoly/blog/368881 Android 平台下使用 i2c-tools Andorid 开发板为 Freescale imx6 的 S ...

  3. Android平台下利用zxing实现二维码开发

    Android平台下利用zxing实现二维码开发 现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研究了下二维码开发的东西,开源的二维码扫描库主要有zxing和zbar,zbar在iPos平 ...

  4. 在Android平台下搭建PhoneGap开发环境--用HTML5开发游戏

    一.在Android平台下搭建PhoneGap开发环境具体怎么搭建我这里就不详细说了,如有需要我后面再讲 . PhoneGap 官方地址有详细说明:http://www.phonegap.com. 在 ...

  5. Android平台下渗透测试工具大集合

    Android平台下渗透测试工具大集合 分享一个google的项目,各种Android下的渗透测试工具. Ad Network Detector (1.2): http://market.androi ...

  6. (转)android平台下使用点九PNG技术

    “点九”是andriod平台的应用软件开发里的一种特殊的图片形式,文件扩展名为:.9.png 智能手机中有自动横屏的功能,同一幅界面会在随着手机(或平板电脑)中的方向传感器的参数不同而改变显示的方向, ...

  7. (转载)Android平台下利用zxing实现二维码开发

    Android平台下利用zxing实现二维码开发 现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研究了下二维码开发的东西,开源的二维码扫描库主要有zxing和zbar,zbar在iPos平 ...

  8. Android平台的音乐资源管理与播放

    Android平台基于Linux和开放手机联盟(OHA)系统,经过中国移动的创新研发,设计出拥有新颖独特的用户操作界面,增强了浏览器能力和WAP 兼容性,优化了多媒体领域的OpenCORE.浏览器领域 ...

  9. Android平台下使用lua调用Java代码经验总结

    动态语言以其执行的灵活性,可配置性.方便调试能够为开发带来极大的方便.假设用好了.能够极大的提高开发的效率. 怪不得像游戏开发这样复杂的软件开发里没有不集成脚本语言的. 当中,lua以其小巧,灵活.方 ...

随机推荐

  1. 主题:PageRank解释

    转自:http://www.iteye.com/topic/95079 PageRank解释 通过对由超过 50,000 万个变量和 20 亿个词汇组成的方程进行计算,PageRank 能够对网页的重 ...

  2. poj 2485 Highways(最小生成树,基础,最大边权)

    题目 //听说听木看懂之后,数据很水,我看看能不能水过 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stri ...

  3. 恢复mdf文件到数据库方法

    CREATE DATABASE crm_testdb1 ON (FILENAME = N'C:\e527051\crm_testdb\crm_testdb_20121104.mdf')FOR ATTA ...

  4. Project Euler 87 :Prime power triples 素数幂三元组

    Prime power triples The smallest number expressible as the sum of a prime square, prime cube, and pr ...

  5. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  6. ibatis动态查询

    在复杂查询过程中,我们常常需要根据用户的选择决定查询条件,这里发生变化的并不只是SQL 中的参数,包括Select 语句中所包括的字段和限定条件,都可能发生变化.典型情况,如在一个复杂的组合查询页面, ...

  7. 转一个distinct用法,很有帮助

    转一个distinct用法,很有帮助 (2011-12-01 15:18:11) 转载▼ 标签: 杂谈 分类: mysql复制 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提 ...

  8. mysql建表时拆分出常用字段和不常用字段

    一对一 一张表的一条记录一定只能与另外一张表的一条记录进行对应,反之亦然. 学生表:姓名,性别,年龄,身高,体重,籍贯,家庭住址,紧急联系人 其中姓名.性别.年龄.身高,体重属于常用数据,但是籍贯.住 ...

  9. Swift入门(十一)——类型转换与is、as操作

    三种操作:is.as?和as! Swift是强类型语言,但也允许开发者通过is.as?和as!这三种操作来对类型进行判断和强制转换.其中is用作类型判断,而as?和as!则分别是类型转换的可选形式和强 ...

  10. ipconfig

    当使用ipconfig时不带任何参数选项,那么它为每个已经配置了的接口显示IP地址.子网掩码和缺省网关值. 如果你安装了虚拟机和无线网卡的话,它们的相关信息也会出现在这里.