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

Android实现自制和播放录音程序,第1张

概述首先,让我们先看下实现的截图:当有录音文件存在时,会显示在下面的ListView当中。

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

当有录音文件存在时,会显示在下面的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"/> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android实现自制和播放录音程序全部内容,希望文章能够帮你解决Android实现自制和播放录音程序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1148039.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存