Android平台下实现录音及播放录音功能的简介
录音及播放的方法如下:
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平台下实现录音及播放录音功能的简介的更多相关文章
- 【转】Android平台下利用zxing实现二维码开发
http://www.cnblogs.com/dolphin0520/p/3355728.html 现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研究了下二维码开发的东西,开源的二维码扫描 ...
- 【转】Android 平台下使用 i2c-tools
原文网址:http://my.oschina.net/luoly/blog/368881 Android 平台下使用 i2c-tools Andorid 开发板为 Freescale imx6 的 S ...
- Android平台下利用zxing实现二维码开发
Android平台下利用zxing实现二维码开发 现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研究了下二维码开发的东西,开源的二维码扫描库主要有zxing和zbar,zbar在iPos平 ...
- 在Android平台下搭建PhoneGap开发环境--用HTML5开发游戏
一.在Android平台下搭建PhoneGap开发环境具体怎么搭建我这里就不详细说了,如有需要我后面再讲 . PhoneGap 官方地址有详细说明:http://www.phonegap.com. 在 ...
- Android平台下渗透测试工具大集合
Android平台下渗透测试工具大集合 分享一个google的项目,各种Android下的渗透测试工具. Ad Network Detector (1.2): http://market.androi ...
- (转)android平台下使用点九PNG技术
“点九”是andriod平台的应用软件开发里的一种特殊的图片形式,文件扩展名为:.9.png 智能手机中有自动横屏的功能,同一幅界面会在随着手机(或平板电脑)中的方向传感器的参数不同而改变显示的方向, ...
- (转载)Android平台下利用zxing实现二维码开发
Android平台下利用zxing实现二维码开发 现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研究了下二维码开发的东西,开源的二维码扫描库主要有zxing和zbar,zbar在iPos平 ...
- Android平台的音乐资源管理与播放
Android平台基于Linux和开放手机联盟(OHA)系统,经过中国移动的创新研发,设计出拥有新颖独特的用户操作界面,增强了浏览器能力和WAP 兼容性,优化了多媒体领域的OpenCORE.浏览器领域 ...
- Android平台下使用lua调用Java代码经验总结
动态语言以其执行的灵活性,可配置性.方便调试能够为开发带来极大的方便.假设用好了.能够极大的提高开发的效率. 怪不得像游戏开发这样复杂的软件开发里没有不集成脚本语言的. 当中,lua以其小巧,灵活.方 ...
随机推荐
- 数据库范式(1NF 2NF 3NF BCNF)
http://blog.csdn.net/xuxurui007/article/details/7738330 http://www.cnblogs.com/laodao1/archive/2009/ ...
- _beginthreadex创建多线程详解
一.需要的头文件支持 #include <process.h> // for _beginthread() 需要的设置:ProjectSetting-->C/C++- ...
- Chp10: Scalability and Memory Limits
The Step-by-Step Approach break down a tricky problem and to solve problems using what you do know. ...
- POJ 2568/ZOJ 1965 Decode the Tree
题意:在树中,每次删去节点值最小的叶子结点. 每删去一个点,就给出与这相连的点的值,直到最后只剩下一个根结点,给这N-1个数,重新建立这个树. 思路: 给出的节点号按次序存入到数组a中,将未给出的数存 ...
- IP地址总结
1.网际协议IP : 网际协议 IP 是 TCP/IP 体系中两个最主要的协议之一.与 IP 协议配套使用的还有四个协议: 地址解析协议 ARP (Address Resolution Protoco ...
- [SQL Server系] -- 约束
什么是约束? 约束(Constraint)是SQL Server中提供的 自动保存数据库完整性 的一种方法,定义了可输入表或表的列中的数据限制条件. SQL Server中共有5中约束 PRIMARY ...
- 一个很好的php分词类库
PHPAnalysis源程序下载与演示: PHP分词系统 V2.0 版下载 | PHP分词系统演示 | PHPAnalysis类API文档 原文连接地址:http://www.phpbone.co ...
- 荣耀3X畅玩版狙击红米note!
华为与小米的战争史,可以追溯到 红米 799,华为独立初荣耀品牌推出荣耀3C,定价798,就是比你少一快; 小米3 定价1999,华为出荣耀3X,定价1980; 最近的红米Note,标配版定价799, ...
- java实现大数加法、乘法(BigDecimal)
之前写过用vector.string实现大数加法,现在用java的BigDecimal类,代码简单很多.但是在online-judge上,java的代码运行时间和内存大得多. java大数加法:求a+ ...
- ORA-12560: TNS: 协议适配器错误 解决
传统方法: 大多数网站都有记载三个方法解决 但是出现上述错误另一个原因: 刚刚安装的Oracle 里面的账户都是锁定的 需要解锁 见如下的Oracle11g安装过程 http://www.2cto.c ...