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<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"/>

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

时间: 2024-08-03 13:29:08

Android实现自制和播放录音程序的相关文章

拨打电话-Android实接听后播放录音给对方听

问题描述 Android实接听后播放录音给对方听 求个思路?或者Demo.好像接听了电话后,播放音乐对方是听不到的. 解决方案 你的手机有回声消除功能.为什么不用微信直接发送呢. 解决方案二: 是不能,如果能那不是就可以听到打电话时对方的声音

android电话接听自动播放录音给对方

问题描述 android电话接听自动播放录音给对方 关于android开发问题:问题如题目,电话接听状态前的代码已经写好了(也就是电话现在已经接通了,我需要怎么播放录音给对方听),请问播放录音这个功能要怎么才能做到 解决方案 大神们呢??难道就没有人做过相关的么 解决方案二: import java.io.File; import java.io.IOException; import android.app.Service; import android.content.Intent; imp

Android编程开发录音和播放录音简单示例_Android

本文实例讲述了Android编程开发录音和播放录音的方法.分享给大家供大家参考,具体如下: /* * The application needs to have the permission to write to external storage * if the output file is written to the external storage, and also the * permission to record audio. These permissions must be

Android编程开发录音和播放录音简单示例

本文实例讲述了Android编程开发录音和播放录音的方法.分享给大家供大家参考,具体如下: /* * The application needs to have the permission to write to external storage * if the output file is written to the external storage, and also the * permission to record audio. These permissions must be

用Delphi制作录音程序

Delphi是Inprise(前Borland)公司的优秀的可视化编程工具,它自带的Mediaplayer控件是开发多媒体的利器.用它几分钟就可以做出一个象解霸一样可以播放多媒体文件的程序来.但可能很少人知道,用它也可以做一个录音程序. 运行Delphi,在System页拖一个Mediaplayer控件到窗体上,默认名为Mediaplayer1.由于我们的程序是采用自己的按钮,所以将Mediaplayer1的Visible属性设置为False,其它属性保持默认值.再放两个按钮Button1和Bu

Android实现的截屏小程序示例_Android

本文实例讲述了Android实现的截屏小程序.分享给大家供大家参考,具体如下: 先看截图,不过这个截屏还不够完整,头上的statusbar没有,呈黑色. 多按了几次,就成这样了,呵呵. package com.test; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Bitmap.Config; import

在android系统中如何播放、暂停、停止歌曲?

问题描述 在android系统中如何播放.暂停.停止歌曲? 我在程序中设置了播放.暂停.停止三个按钮.当我点击播放按钮,这个按钮就消失了,显示的是暂停按钮.反之亦然.当我点击播放按钮时,程序不出错.再点击暂停按钮时,就会出错.贴出代码: package com.mpIlango; import java.io.IOException; import java.util.ArrayList; import android.app.Activity; import android.media.Med

android webview视频不能播放

问题描述 android webview视频不能播放 各位大神,在用webview加载一个html网页的时候(html网页和视频均在sd内),视屏有时候能播放有时候不能播放,有时候多次退出网页重新进入网页播放视屏的时候,只有声音没有图像,请问这个该怎么解决. 解决方案 android webview 播放视频bugfix:录音的时候,webview不能播放视频WebView播放视频 解决方案二: 可能是网速原因或者那是声音不是视频 解决方案三: 一些缓存属性什么的是否进行设置 解决方案四: 既然

手把手教你Android来去电通话自动录音的方法

原文:手把手教你Android来去电通话自动录音的方法 http://www.jizhuomi.com/android/example/354.html 我们在使用Android手机打电话时,有时可能会需要对来去电通话自动录音,本文就详细讲解实现Android来去电通话自动录音的方法,大家按照文中的方法编写程序就可以完成此功能.        来去电自动录音的关键在于如何监听手机电话状态的转变:        1)来电的状态的转换如下(红色标记是我们要用到的状态)        空闲(IDEL)