热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

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

这篇文章主要为大家详细介绍了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;

 /** 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(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<&#63;> 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();
  if (sdCardExit)
  {
   File files[] = myRecAudioDir.listFiles();
   if (files != null)
   {

    for (int i = 0; i = 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.总体布局文件代码

<&#63;xml version="1.0" encoding="utf-8"&#63;>

 
 
 
 
 
 
 
 
 
 
 
 
 
 


3.ListView中的子View的布局

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
 

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


推荐阅读
  • 本文详细解析了如何利用Appium与Python在真实设备上执行测试示例的方法。首先,需要开启手机的USB调试功能;其次,通过数据线将手机连接至计算机并授权USB调试权限。最后,在命令行工具中验证设备连接状态,确保一切准备就绪,以便顺利进行测试。 ... [详细]
  • 本文深入探讨了 AdoDataSet RecordSet 的序列化与反序列化技术,详细解析了将 RecordSet 转换为 XML 格式的方法。通过使用 Variant 类型变量和 TStringStream 流对象,实现数据集的高效转换与存储。该方法不仅提高了数据传输的灵活性,还增强了数据处理的兼容性和可扩展性。 ... [详细]
  • EasyUI作为一种高效的前端框架,显著简化了JavaScript代码的编写,提升了开发效率。在构建窗口应用程序时,首先需要引入EasyUI所需的JS文件和CSS样式表。由于EasyUI依赖于jQuery,因此还需确保正确加载jQuery库。通过这种方式,开发者能够快速实现界面组件的动态交互与美观布局,为用户提供更加流畅的使用体验。 ... [详细]
  • 第一次写这玩意,不知道什么时候能写完,今天项目比较近,期望年底能看完吧。先定个小目标20201228完成第1章Spring介绍第2章入门第3章在Spring中引入IoC和DI第4章 ... [详细]
  • classpath和classpath*区别:classpath:只会到你的class路径中查找找文件。classpath*:不仅包含class路径,还包括jar文件中(class ... [详细]
  • P31 系统全面升级与PUT功能增强
    在P31系统的全面升级中,PUT功能得到了显著增强。具体而言,系统现在支持通过传递一组ID来批量更新或替换公司信息,但这种操作的实际应用场景较为有限,因为其影响范围较大。为了确保数据的安全性和准确性,建议仅在必要时使用此功能,并严格控制更新或新增URI对应资源的权限。 ... [详细]
  • 如何在SharePoint 2013中使用不同用户身份进行登录操作
    在创建了SharePoint 2013网站后,我注意到其界面与2010版本有所不同,特别是缺少了“以其他用户身份登录”的功能,这对测试工作造成了不便。通过查阅一些国外的技术资源,最终找到了有效的解决方案。这一方法不仅解决了登录问题,还提升了多用户环境下的测试效率和安全性。 ... [详细]
  • 利用IDEA高效构建Maven Spring MVC项目环境
    本文详细介绍了如何使用IntelliJ IDEA高效搭建Maven Spring MVC项目环境。首先,通过创建一个新的Maven项目,设置好GroupId、ArtifactId和Version等基本信息。接着,配置项目的依赖和插件,确保Spring MVC框架能够顺利集成。最后,通过IDEA的内置工具完成项目的初始化和测试,为后续开发打下坚实基础。 ... [详细]
  • ### 一、指令概述指令是 Vue 中的一种特殊属性,用于增强 HTML 元素的功能。它们以 `v-` 开头,如 `v-cloak`。### 二、`v-cloak` 指令的应用`v-cloak` 指令主要用于解决页面加载过程中未编译的 Vue 插值表达式短暂显示的问题。在 Vue 实例编译完成之前,带有 `v-cloak` 的元素将被隐藏,从而避免了“闪动”现象。通过结合 CSS 样式,可以进一步优化用户体验,确保页面在初始加载时保持整洁和专业。 ... [详细]
  • 学术论文深度解析与评价
    本文深入探讨了基于摆线推进器的无人监测船系统的研发背景及其重要性。从环境保护的宏观视角出发,逐步聚焦至湖泊生态监测的具体需求,分析了现有生态监测技术的局限性,并提出了创新性的解决方案,旨在通过改进内部技术实现更高效、精准的生态环境监测。 ... [详细]
  • 将 Eclipse 中的 Java Web 项目迁移至 IntelliJ IDEA 并配置 Tomcat 环境
    为了适应更高效的工作流程,本文详细介绍了如何将基于Eclipse构建的Java Web项目迁移到IntelliJ IDEA,并在新环境中配置Tomcat服务器,以确保项目的顺利运行。此过程不仅涉及项目文件的转移,还包括解决可能遇到的兼容性问题和环境配置挑战。通过本文的指导,开发者可以轻松实现从Eclipse到IntelliJ IDEA的过渡,提升开发效率。 ... [详细]
  • 【高效构建全面的iOS直播应用】(美颜功能深度解析)
    本文深入探讨了如何高效构建全面的iOS直播应用,特别聚焦于美颜功能的技术实现。通过详细解析美颜算法和优化策略,帮助开发者快速掌握关键技术和实现方法,提升用户体验。适合对直播应用开发感兴趣的开发者阅读。 ... [详细]
  • OpenCV 2.4.9 源码解析:级联分类器的错误率与尺寸分析 ... [详细]
  • 本文探讨了Node.js Cluster模块在多核CPU环境下的应用及其性能测试。通过安装`async`包并利用Node.js自带的`http`和`cluster`模块,创建了一个名为`cluster.js`的文件,该文件根据系统CPU核心数动态生成多个工作进程,以实现负载均衡和提高应用性能。实验结果表明,使用Cluster模块能够显著提升高并发场景下的响应速度和处理能力。 ... [详细]
  • 通过Apache Commons FileUpload组件,可以根据具体应用需求实现多样化的文件上传功能。在基本应用场景中,开发者可以通过调用单一方法来解析Servlet请求,从而轻松处理文件上传任务。此外,该组件还提供了丰富的配置选项和高级功能,支持大文件上传、多文件并发处理等复杂场景,显著提升了文件上传的效率和可靠性。 ... [详细]
author-avatar
YYYan1023
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有