当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现自制和播放录音程序

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

马馥芳,芊孜,wonderflower

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

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网