- 使用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"/>
- 布局就不用说了 ,附上代码吧
- 主要代码:
- 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()
- + "/RECORDERS";
- 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);
- Log.e("activity", saveFilePath+"");
- 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://www.phpxs.com/code/1001835/