Android 는 녹음 프로그램 을 자체 제작 하고 재생 합 니 다.

11133 단어 Android녹음 하 다.
우선 실 현 된 캡 처 를 살 펴 보 자.

녹음 파일 이 존재 할 때 아래 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;

    } catch (IOException e)
    {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }

   }
  });
  /*    */
  myButton2.setOnClickListener(new ImageButton.OnClickListener()
  {

   @Override
   public void onClick(View arg0)
   {
    // TODO Auto-generated method stub
    if (myRecAudioFile != null)
    {
     /*      */
     mMediaRecorder01.stop();
     /*        Adapter */
     adapter.add(myRecAudioFile.getName());
     mMediaRecorder01.release();
     mMediaRecorder01 = null;
     myTextView1.setText("  :" + myRecAudioFile.getName());

     myButton2.setEnabled(false);

     isStopRecord = true;
    }
   }
  });
  /*    */
  myButton3.setOnClickListener(new ImageButton.OnClickListener()
  {

   @Override
   public void onClick(View arg0)
   {
    // TODO Auto-generated method stub
    if (myPlayFile != null && myPlayFile.exists())
    {
     /*         */
     openFile(myPlayFile);
    }

   }
  });
  /* ?  */
  myButton4.setOnClickListener(new ImageButton.OnClickListener()
  {

   @Override
   public void onClick(View arg0)
   {
    // TODO Auto-generated method stub
    if (myPlayFile != null)
    {
     /*   Adapter      */
     adapter.remove(myPlayFile.getName());
     /*      */
     if (myPlayFile.exists())
      myPlayFile.delete();
     myTextView1.setText("    ");
    }

   }
  });

  myListView1.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1,
       int arg2, long arg3)
     {
      /*                 Enable */
      myButton3.setEnabled(true);
      myButton4.setEnabled(true);

      myPlayFile = new File(myRecAudioDir.getAbsolutePath()
        + File.separator
        + ((CheckedTextView) arg1).getText());
      myTextView1.setText("    :"
        + ((CheckedTextView) arg1).getText());
     }
    });

 }

 @Override
 protected void onStop()
 {
  if (mMediaRecorder01 != null && !isStopRecord)
  {
   /*      */
   mMediaRecorder01.stop();
   mMediaRecorder01.release();
   mMediaRecorder01 = null;
  }
  super.onStop();
 }

 private void getRecordFiles()
 {
  recordFiles = new ArrayList<String>();
  if (sdCardExit)
  {
   File files[] = myRecAudioDir.listFiles();
   if (files != null)
   {

    for (int i = 0; i < files.length; i++)
    {
     if (files[i].getName().indexOf(".") >= 0)
     {
      /*   .amr   */
      String fileS = files[i].getName().substring(
        files[i].getName().indexOf("."));
      if (fileS.toLowerCase().equals(".amr"))
       recordFiles.add(files[i].getName());

     }
    }
   }
  }
 }

 /*             */
 private void openFile(File f)
 {
  Intent intent = new Intent();
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  intent.setAction(android.content.Intent.ACTION_VIEW);

  String type = getMIMEType(f);
  intent.setDataAndType(Uri.fromFile(f), type);
  startActivity(intent);
 }

 private String getMIMEType(File f)
 {
  String end = f.getName().substring(
    f.getName().lastIndexOf(".") + 1, f.getName().length())
    .toLowerCase();
  String type = "";
  if (end.equals("mp3") || end.equals("aac") || end.equals("aac")
    || end.equals("amr") || end.equals("mpeg")
    || end.equals("mp4"))
  {
   type = "audio";
  } else if (end.equals("jpg") || end.equals("gif")
    || end.equals("png") || end.equals("jpeg"))
  {
   type = "image";
  } else
  {
   type = "*";
  }
  type += "/*";
  return type;
 }
}
2.전체 레이아웃 파일 코드

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" 
 android:background="@drawable/white">
 <LinearLayout 
 android:id="@+id/LinearLayout01" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content">
 <ImageButton 
 android:id="@+id/ImageButton01" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:src="@drawable/record">
 </ImageButton>
 <ImageButton 
 android:id="@+id/ImageButton02" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:src="@drawable/stop">
 </ImageButton>
 <ImageButton 
 android:id="@+id/ImageButton03" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:src="@drawable/play">
 </ImageButton>
 <ImageButton 
 android:id="@+id/ImageButton04" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:src="@drawable/delete">
 </ImageButton>
 </LinearLayout>
 <TextView 
 android:id="@+id/TextView01" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textColor="@drawable/black">
 </TextView>
 <ListView 
 android:id="@+id/ListView01" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:background="@drawable/black">
 </ListView>
</LinearLayout>

3.ListView 의 하위 View 레이아웃

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/myCheckedTextView1" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:textColor="@drawable/white"/> 
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기