实现自制录音/播放录音程序

首先,让我们先看下实现的截图:

实现自制录音/播放录音程序

当有录音文件存在时,会显示在下面的ListView当中。

下面给出实现的完整代码:

1.主程序代码

package irdc.ex07_11;

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

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.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class EX07_11 extends Activity
{
  private ImageButton myButton1;
  private ImageButton myButton2;
  private ImageButton myButton3;
  private ImageButton myButton4;
  private ListView myListView1;
  private String strTempFile = "ex07_11_";
  private File myRecAudioFile;
  private File myRecAudioDir;
  private File myPlayFile;
  private MediaRecorder mMediaRecorder01;

private ArrayList<String> recordFiles;
  private ArrayAdapter<String> adapter;
  private TextView myTextView1;
  private boolean sdCardExit;
  private boolean isStopRecord;

/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

myButton1 = (ImageButton) findViewById(R.id.ImageButton01);
    myButton2 = (ImageButton) findViewById(R.id.ImageButton02);
    myButton3 = (ImageButton) findViewById(R.id.ImageButton03);
    myButton4 = (ImageButton) findViewById(R.id.ImageButton04);
    myListView1 = (ListView) findViewById(R.id.ListView01);
    myTextView1 = (TextView) findViewById(R.id.TextView01);
    myButton2.setEnabled(false);
    myButton3.setEnabled(false);
    myButton4.setEnabled(false);

/* 判断SD Card是否插入 */
    sdCardExit = Environment.getExternalStorageState().equals(
        android.os.Environment.MEDIA_MOUNTED);
    /* 取得SD Card路径做为录音的文件位置 */
    if (sdCardExit)
      myRecAudioDir = Environment.getExternalStorageDirectory();

/* 取得SD Card目录里的所有.amr文件 */
    getRecordFiles();

adapter = new ArrayAdapter<String>(this,
        R.layout.my_simple_list_item, recordFiles);
    /* 将ArrayAdapter存入ListView对象中 */
    myListView1.setAdapter(adapter);

/* 录音 */
    myButton1.setOnClickListener(new ImageButton.OnClickListener()
    {

@Override
      public void onClick(View arg0)
      {
        try
        {
          if (!sdCardExit)
          {
            Toast.makeText(EX07_11.this, "请插入SD Card",
                Toast.LENGTH_LONG).show();
            return;
          }

/* 建立录音档 */
          myRecAudioFile = File.createTempFile(strTempFile, ".amr",
              myRecAudioDir);

mMediaRecorder01 = new MediaRecorder();
          /* 设定录音来源为麦克风 */
          mMediaRecorder01
              .setAudioSource(MediaRecorder.AudioSource.MIC);
          mMediaRecorder01
              .setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
          mMediaRecorder01
              .setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

mMediaRecorder01.setOutputFile(myRecAudioFile
              .getAbsolutePath());

mMediaRecorder01.prepare();

mMediaRecorder01.start();

myTextView1.setText("录音中");

myButton2.setEnabled(true);
          myButton3.setEnabled(false);
          myButton4.setEnabled(false);

isStopRecord = false;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/e4ad31ace4a5a41dc0102e6557eb9a72.html