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以其小巧,灵活.方 ...
随机推荐
- cg 到hlsl的转换
http://msdn.microsoft.com/en-us/library/windows/desktop/ff471376(v=vs.85).aspx http://gamedev.stacke ...
- 如何解决VS启动越来越慢
VS2013 用久后,现在启动和打开项目变得很慢 解决方案: A.清理缓存 VS2010清理缓存:启用vs2010命令行工具:在vs2010命令提示符下,执行devenv.exe /resetuser ...
- 在IDEA上用python来连接集群上的hive
1.在使用Python连接hive之前需要将hive中的文件拷贝到自己创建python项目中 cp -r apache-hive--bin/lib/py /home/jia/Desktop 2.把h ...
- 使用git代替FTP部署代码到服务器的例子
这篇文章主要介绍了使用git代替FTP部署代码到服务器的例子,这种方法可以节省流量.节省时间,需要的朋友可以参考下 本地开发完成后,通常会在服务器上部署,有人会使用ftp,有人会使用scp, ftp和 ...
- Xamarin for Visual Studio 3.11.658 Alpha 版 破解补丁
注意:此版本为 Alpha 版,版本迭代较频繁,仅供尝鲜 前提概要 全新安装请参考 安装 Xamarin for Visual Studio. 最新稳定版请参考 Xamarin for Visual ...
- TDD 用语
OOP 封装 继承 多态 SOLID SRP 单一职责 Single Responsibility Principle OCP 开放封闭 Open/Close Principle LS ...
- Win7 下硬盘安装Linux Mint 17
下载Linux Mint 17镜像,放到C盘根目录:解压出mint.iso文件中casper目录下的vmliunz和initrd.lz两个文件,同样放在C盘的根目录里. 在Win7上安装EasyBCD ...
- lintcode:Plus One 加一
题目: 加一 给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组. 该数字按照大小进行排列,最大的数在列表的最前面. 样例 给定 [1,2,3] 表示 123, 返回 [1,2,4 ...
- AutoEventWireup解释
这一事件聚合了当前页是否自动关联某些特殊事件. 首先,从浏览器页面出发的事件不能立刻在本地得到处理,而是POST至服务器上,因此,asp.net建立了委托(代理)机制.在建立一个事件的同事,建立相应的 ...
- css元素z-index设置为什么不起作用?
元素位置重叠的背景常识 (x)html文档中的元素默认处于普通流(normal flow)中,也就是说其顺序由元素在文档中的先后位置决定,此时一般不会产生重叠(但指定负边距可能产生重叠). 当我们用c ...