Android MediaRecorder录制音频


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Aina.Android" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Test" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-permission android:name="android.permission.RECORD_AUDIO">
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

</manifest>




package com.Aina.Android;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Test extends Activity {
/** Called when the activity is first created. */
private ListView mListView = null;
private Button btn_start = null;
private Button btn_stop = null;
private MediaRecorder mMediaRecorder = null;
private List<String> rec = new ArrayList<String>();// 存放录音文件
private File home = null;
private File path = null;
private String temp = "recaudio_";// 临时文件前缀

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) this.findViewById(R.id.ListView01);
btn_start = (Button) this.findViewById(R.id.Button01);
btn_stop = (Button) this.findViewById(R.id.Button02);
// 是否存在SD卡
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
home = Environment.getExternalStorageDirectory();
MusicList();
} else {
Toast.makeText(this, "请先插入SD卡", Toast.LENGTH_LONG).show();
return;
}
btn_start.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
try {
// 创建录音临时文件
path = File.createTempFile(temp, ".amr", home);
setTitle("=="+path.getAbsolutePath());
mMediaRecorder = new MediaRecorder();

mMediaRecorder
.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置数据来源,麦克风
mMediaRecorder
.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);// 设置格式
mMediaRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);// 设置编码
mMediaRecorder.setOutputFile(path.getAbsolutePath());// 设置输出文件路径
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

});
btn_stop.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
MusicList();
}

});
mListView.setOnItemClickListener(new OnItemClickListener(){

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String path = home+File.separator+rec.get(arg2);
File f = new File(path);
PlayMusic(f);
}

});
}

/**
* 显示列表
*/
public void MusicList() {
File[] f = home.listFiles(new MusicFilter());
rec.clear();
for (int i = 0; i < f.length; i++) {
File file = f[i];
rec.add(file.getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, rec);
mListView.setAdapter(adapter);
}

/**
* 播放录音文件
* @param file
*/
public void PlayMusic(File file){
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "audio");
this.startActivity(intent);
}
class MusicFilter implements FilenameFilter {

public boolean accept(File dir, String filename) {
// TODO Auto-generated method stub
return (filename.endsWith(".amr"));
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值