使用MediaRecorder的步骤:
1、创建MediaRecorder对象
2、调用MediRecorder对象的setAudioSource()方法设置声音的来源,一般传入MediaRecorder.MIC
3、调用MediaRecorder对象的setoutputFormat()设置所录制的音频文件的格式
4、调用MediaRecorder对象的setAudioRncoder()、setAudioEnCodingBitRate(int bitRate)、setAudioSamlingRate(int SamplingRate)设置所录音的编码格式、编码位率、采样率等,
5、调用MediaRecorder对象的setoutputfile(String path)方法设置录制的音频文件的保存位置
6、调用MediaRecoder对象的Prepare()方法准备录制
7、调用MediaRecoder对象的start()方法开始录制
8、调用MediaRecoder对象的stop()方法停止录制,并调用release()方法释放资源
实例:
复制代码 代码如下:
<uses-permission androID:name="androID.permission.MOUNT_FORMAT_fileSYstemS"/>
<uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission androID:name="androID.permission.RECORD_AUdio"/>
@L_502_1@ 代码如下:
<relativeLayout 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"
tools:context=".MainActivity" >
<linearLayout
androID:ID="@+ID/li1"
androID:orIEntation="horizontal"
androID:layout_wIDth="match_parent"
androID:layout_height="wrap_content">
<button androID:ID="@+ID/start"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_weight="1"
androID:text="@string/start"/>
<button androID:ID="@+ID/stop"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_weight="1"
androID:text="@string/stop"/>
</linearLayout>
<ListVIEw
androID:ID="@+ID/List"
androID:layout_below="@ID/li1"
androID:layout_wIDth="match_parent"
androID:layout_height="wrap_content"></ListVIEw>
</relativeLayout>
复制代码 代码如下:
<?xml version="1.0" enCoding="utf-8"?>
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:layout_wIDth="match_parent"
androID:layout_height="match_parent"
androID:orIEntation="horizontal" >
<TextVIEw
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:ID="@+ID/show_file_name" />
<button
androID:ID="@+ID/bt_List_play"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:text="@string/play"/>
<button androID:ID="@+ID/bt_List_stop"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:text="@string/List_stop"/>
</linearLayout>
复制代码 代码如下:
package com.androID.xiong.mediarecordertest;
import java.io.file;
import java.io.IOException;
import java.text.SimpleDateFormat;
import androID.app.Activity;
import androID.app.AlertDialog;
import androID.app.AlertDialog.Builder;
import androID.app.Dialog;
import androID.content.DialogInterface;
import androID.media.MediaPlayer;
import androID.media.MediaRecorder;
import androID.os.Bundle;
import androID.os.Environment;
import androID.vIEw.LayoutInflater;
import androID.vIEw.Menu;
import androID.vIEw.VIEw;
import androID.vIEw.VIEw.OnClickListener;
import androID.vIEw.VIEwGroup;
import androID.Widget.BaseAdapter;
import androID.Widget.button;
import androID.Widget.EditText;
import androID.Widget.ListVIEw;
import androID.Widget.TextVIEw;
public class MainActivity extends Activity implements OnClickListener {
private button start;
private button stop;
private ListVIEw ListVIEw;
// 录音文件播放
private MediaPlayer myPlayer;
// 录音
private MediaRecorder myRecorder;
// 音频文件保存地址
private String path;
private String paths = path;
private file savefilePath;
// 所录音的文件
String[] Listfile = null;
ShowRecorderAdpter showRecord;
AlertDialog aler = null;
@OverrIDe
protected voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.activity_main);
start = (button) findVIEwByID(R.ID.start);
stop = (button) findVIEwByID(R.ID.stop);
ListVIEw = (ListVIEw) findVIEwByID(R.ID.List);
myPlayer = new MediaPlayer();
myRecorder = new MediaRecorder();
// 从麦克风源进行录音
myRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAulT);
// 设置输出格式
myRecorder.setoutputFormat(MediaRecorder.OutputFormat.DEFAulT);
// 设置编码格式
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAulT);
showRecord = new ShowRecorderAdpter();
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
try {
path = Environment.getExternalStorageDirectory()
.getCanonicalPath().toString()
+ "/XIONGRECORDERS";
file files = new file(path);
if (!files.exists()) {
files.mkdir();
}
Listfile = files.List();
} catch (IOException e) {
e.printstacktrace();
}
}
start.setonClickListener(this);
stop.setonClickListener(this);
if (Listfile != null) {
ListVIEw.setAdapter(showRecord);
}
}
@OverrIDe
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
class ShowRecorderAdpter extends BaseAdapter {
@OverrIDe
public int getCount() {
return Listfile.length;
}
@OverrIDe
public Object getItem(int arg0) {
return arg0;
}
@OverrIDe
public long getItemID(int arg0) {
return arg0;
}
@OverrIDe
public VIEw getVIEw(final int postion,VIEw arg1,VIEwGroup arg2) {
VIEw vIEws = LayoutInflater.from(MainActivity.this).inflate(
R.layout.List_show_filerecorder,null);
TextVIEw filename = (TextVIEw) vIEws
.findVIEwByID(R.ID.show_file_name);
button plays = (button) vIEws.findVIEwByID(R.ID.bt_List_play);
button stop = (button) vIEws.findVIEwByID(R.ID.bt_List_stop);
filename.setText(Listfile[postion]);
// 播放录音
plays.setonClickListener(new OnClickListener() {
@OverrIDe
public voID onClick(VIEw arg0) {
try {
myPlayer.reset();
myPlayer.setDataSource(path + "/" + Listfile[postion]);
if (!myPlayer.isPlaying()) {
myPlayer.prepare();
myPlayer.start();
} else {
myPlayer.pause();
}
} catch (IOException e) {
e.printstacktrace();
}
}
});
// 停止播放
stop.setonClickListener(new OnClickListener() {
@OverrIDe
public voID onClick(VIEw arg0) {
if (myPlayer.isPlaying()) {
myPlayer.stop();
}
}
});
return vIEws;
}
}
@OverrIDe
public voID onClick(VIEw v) {
switch (v.getID()) {
case R.ID.start:
final EditText filename = new EditText(this);
Builder alerBuIDler = new Builder(this);
alerBuIDler
.setTitle("请输入要保存的文件名")
.setVIEw(filename)
.setPositivebutton("确定",
new DialogInterface.OnClickListener() {
@OverrIDe
public voID onClick(DialogInterface dialog,
int which) {
String text = filename.getText().toString();
try {
paths = path
+ "/"
+ text
+ new SimpleDateFormat(
"yyyyMMddHHmmss").format(System
.currentTimeMillis())
+ ".amr";
savefilePath = new file(paths);
myRecorder.setoutputfile(savefilePath
.getabsolutePath());
savefilePath.createNewfile();
myRecorder.prepare();
// 开始录音
myRecorder.start();
start.setText("正在录音中。。");
start.setEnabled(false);
aler.dismiss();
// 重新读取 文件
file files = new file(path);
Listfile = files.List();
// 刷新ListVIEw
showRecord.notifyDataSetChanged();
} catch (Exception e) {
e.printstacktrace();
}
}
});
aler = alerBuIDler.create();
aler.setCanceledOntouchOutsIDe(false);
aler.show();
break;
case R.ID.stop:
if (savefilePath.exists() && savefilePath != null) {
myRecorder.stop();
myRecorder.release();
// 判断是否保存 如果不保存则删除
new AlertDialog.Builder(this)
.setTitle("是否保存该录音")
.setPositivebutton("确定",null)
.setNegativebutton("取消",
new DialogInterface.OnClickListener() {
@OverrIDe
public voID onClick(DialogInterface dialog,
int which) {
savefilePath.delete();
// 重新读取 文件
file files = new file(path);
Listfile = files.List();
// 刷新ListVIEw
showRecord.notifyDataSetChanged();
}
}).show();
}
start.setText("录音");
start.setEnabled(true);
default:
break;
}
}
@OverrIDe
protected voID onDestroy() {
// 释放资源
if (myPlayer.isPlaying()) {
myPlayer.stop();
myPlayer.release();
}
myPlayer.release();
myRecorder.release();
super.onDestroy();
}
}
源码下载:http://xiazai.jb51.net/201401/yuanma/MediaRecorderTest(jb51.net).rar
总结以上是内存溢出为你收集整理的Android音频录制MediaRecorder之简易的录音软件实现代码全部内容,希望文章能够帮你解决Android音频录制MediaRecorder之简易的录音软件实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)