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

当有录音文件存在时,会显示在下面的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. */@Overridepublic 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(){@Overridepublic 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 blocke.printStackTrace();}}});/* 停止 */myButton2.setOnClickListener(new ImageButton.OnClickListener(){@Overridepublic void onClick(View arg0){// TODO Auto-generated method stubif (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(){@Overridepublic void onClick(View arg0){// TODO Auto-generated method stubif (myPlayFile != null && myPlayFile.exists()){/* 开启播放的程序 */openFile(myPlayFile);}}});/* ?除 */myButton4.setOnClickListener(new ImageButton.OnClickListener(){@Overridepublic void onClick(View arg0){// TODO Auto-generated method stubif (myPlayFile != null){/* 因将Adapter移除文件名 */adapter.remove(myPlayFile.getName());/* 删除文件 */if (myPlayFile.exists())myPlayFile.delete();myTextView1.setText("完成删除");}}});myListView1.setOnItemClickListener(new AdapterView.OnItemClickListener(){@Overridepublic 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());}});}@Overrideprotected 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"/>

4.除了这些,我们还需要加入读取Sd的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Android--实现自制录音/播放录音程序相关推荐

  1. Android实现可录音/暂停录音/播放录音的录音软件

    一.功能简介与操作视频 该APP功能完成音频的录制并命名保存与播放功能 1.录制 在音频录制界面点击开始按钮即可进行录制,录制过程中可以点击暂停按钮暂时停止录制,暂停可以继续录制,点击停止按钮结束录制 ...

  2. iOS语音消息功能实现,录音/播放录音

    // // ViewController.m // VoiceMessage // // Created by 李伟宾 on 2017/11/28. // Copyright © 2017年 liwe ...

  3. android 录音与播放录音 带根据音频大小动态效果

    先看看效果图:         首先是布局文件: <?xml version="1.0" encoding="utf-8"?> <Linear ...

  4. uni —app 录音_uni-app 录音(十六)

    uni.getRecorderManager() 获取全局唯一的录音管理器 recorderManager. 平台支持 小程序 5+APP recorderManager 对象的方法列表 方法 参数 ...

  5. Android开发之PCM录音实时播放的实现方法 | 边录音边播放 |PCM录音播放无延迟 | 录音无杂音 | 录音无噪音

    先说下录音得开启录音权限 <uses-permission android:name="android.permission.RECORD_AUDIO" /> 然后录音 ...

  6. android录音播放并上传

    最近研究了下录音上传,各位有需要可参考下,如有不妥欢迎指出 <pre name="code" class="html">package com.ki ...

  7. 微信小程序开发之——录音播放及文件上传下载-理论(1)

    一 概述 小程序录制音频相关的API--recorderManager 小程序播放音频相关的API--InnerAudioContext 文件的上传相关API--wx.uploadFile 文件的下载 ...

  8. android蓝牙耳机录音播放,android蓝牙耳机录音并播放(二)

    Github下载 package com.example.superb.yy4; import android.content.Context; import android.media.AudioF ...

  9. 微信小程序开发之——录音播放及文件上传下载-示例(2)

    一 概述 开始播放和暂停播放按钮,演示音频的播放和暂停功能 开始播放时,先下载服务器上的文件(1.mp3),然后进行播放 录音.停止.回放按钮,演示录音API的相关功能 上传按钮将录音文件上传到后台h ...

最新文章

  1. 不止摩尔定律,计算领域值得学习的定律还有哪些?
  2. web移动开发最佳实践之js篇
  3. 生活在AI的世界是种什么体验?
  4. mysql range用法_MySQL的常用函数
  5. PowerShell删除Exchange 2010邮件的正确姿势
  6. 校招启动 | 2021 神策未来星全面启航,只差 1 个你!
  7. 在FAANG面试中破解堆算法
  8. c 最大子序列和_最大连续子序列
  9. 第十节:实现vue组件之间的通信
  10. 信息学奥赛一本通(1321:【例6.3】删数问题(Noip1994))
  11. (6)Vivado软件开发流程(第2天)
  12. 好吧,你说简单就简单,但简单的事,不要变成本能,要常思常变
  13. JavaScript 原始数据类型转换
  14. 洛谷——P1420 最长连号
  15. 计算机为什么无法搜索功能,电脑搜索功能不能用怎么办?电脑搜索不能用的解决方法...
  16. html单标签的语法并举例,HTML简介及举例
  17. 什么是铠装光纤跳线及它的特点?
  18. 第十五篇:大球联赛与小球联赛
  19. 《简单法则》读书笔记
  20. 原创 基于微信场地预约小程序 毕业设计 毕设 源码 源代码 欣赏 - 可用于羽毛球、篮球、乒乓、网球等预约小程序

热门文章

  1. css实战笔记(一):写网页前的reset工作
  2. 一种嵌套滑动冲突的解决方案
  3. 工业安全的未来——IT与OT的融合
  4. HDU2425:Hiking Trip(简单bfs,优先队列实现)
  5. UML建模系列文章总结
  6. 实现windows的负载均衡
  7. postman使用介绍
  8. 运营商助力新型智慧城市建设
  9. ASP.NET CORE 1.0 MVC API 文档用 SWASHBUCKLE SWAGGER实现
  10. 当前不会命中断点 源代码与原始版本不一致