栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现自制和播放录音程序

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 recordFiles;
 private ArrayAdapter adapter;
 private TextView myTextView1;
 private boolean sdCardExit;
 private boolean isStopRecord;

 
 @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);

  
  sdCardExit = Environment.getExternalStorageState().equals(
    android.os.Environment.MEDIA_MOUNTED);
  
  if (sdCardExit)
   myRecAudioDir = Environment.getExternalStorageDirectory();

  
  getRecordFiles();

  adapter = new ArrayAdapter(this,
    R.layout.my_simple_list_item, recordFiles);
  
  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.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.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)
     {
      
      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();
  if (sdCardExit)
  {
   File files[] = myRecAudioDir.listFiles();
   if (files != null)
   {

    for (int i = 0; i < files.length; i++)
    {
     if (files[i].getName().indexOf(".") >= 0)
     {
      
      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.总体布局文件代码



 
 
 
 
 
 
 
 
 
 
 
 
 
 


3.ListView中的子View的布局

 
 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/161210.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号