Android實(shí)現(xiàn)自制和播放錄音程序
首先,讓我們先看下實(shí)現(xiàn)的截圖:

當(dāng)有錄音文件存在時(shí),會(huì)顯示在下面的ListView當(dāng)中。
下面給出實(shí)現(xiàn)的完整代碼:
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對(duì)象中 */
myListView1.setAdapter(adapter);
/* 錄音 */
myButton1.setOnClickListener(new ImageButton.OnClickListener()
{
@Override
public void onClick(View arg0)
{
try
{
if (!sdCardExit)
{
Toast.makeText(EX07_11.this, "請(qǐng)插入SD Card",
Toast.LENGTH_LONG).show();
return;
}
/* 建立錄音檔 */
myRecAudioFile = File.createTempFile(strTempFile, ".amr",
myRecAudioDir);
mMediaRecorder01 = new MediaRecorder();
/* 設(shè)定錄音來(lái)源為麥克風(fēng) */
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())
{
/* 開(kāi)啟播放的程序 */
openFile(myPlayFile);
}
}
});
/* ?除 */
myButton4.setOnClickListener(new ImageButton.OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if (myPlayFile != null)
{
/* 因?qū)dapter移除文件名 */
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)
{
/* 當(dāng)有點(diǎn)選文件名時(shí)將刪除及播放按鈕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());
}
}
}
}
}
/* 開(kāi)啟播放錄音文件的程序 */
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"/>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android一個(gè)類(lèi)實(shí)現(xiàn)錄音與播放實(shí)例
- 詳解Android開(kāi)發(fā)錄音和播放音頻的步驟(動(dòng)態(tài)獲取權(quán)限)
- Android實(shí)現(xiàn)語(yǔ)音播放與錄音功能
- Android編程實(shí)現(xiàn)錄音及保存播放功能的方法【附demo源碼下載】
- android語(yǔ)音即時(shí)通訊之錄音、播放功能實(shí)現(xiàn)代碼
- Android 錄音與播放功能的簡(jiǎn)單實(shí)例
- Android使用MediaRecorder實(shí)現(xiàn)錄音及播放
- Android錄音播放管理工具
- Android編程開(kāi)發(fā)錄音和播放錄音簡(jiǎn)單示例
- Android實(shí)現(xiàn)音頻錄音與播放
相關(guān)文章
android模擬器開(kāi)發(fā)和測(cè)試nfc應(yīng)用實(shí)例詳解
本文介紹android模擬器開(kāi)發(fā)nfc應(yīng)用詳解,大家參考使用吧2013-12-12
Gradle?Build?Cache引發(fā)的Task緩存編譯問(wèn)題
這篇文章主要為大家介紹了Gradle?Build?Cache引發(fā)的Task緩存編譯問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
rxjava+retrofit實(shí)現(xiàn)多圖上傳實(shí)例代碼
本篇文章主要介紹了rxjava+retrofit實(shí)現(xiàn)多圖上傳實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Fragment 多層嵌套方法調(diào)用問(wèn)題的解決方案
這篇文章主要介紹了Fragment 多層嵌套方法調(diào)用問(wèn)題的解決方案的相關(guān)資料,需要的朋友可以參考下2016-08-08
Ubuntu 14.04下創(chuàng)建Genymotion安卓虛擬機(jī)的步驟詳解
Android 模擬器一直以速度奇慢無(wú)比著稱(chēng),基本慢到不可用。本文介紹我一直在用的 Genymotion,速度不亞于真機(jī)。而且功能齊全,使用簡(jiǎn)單。下面這篇文章主要介紹了Ubuntu 14.04下創(chuàng)建Genymotion虛擬機(jī)的步驟,需要的朋友可以參考下。2017-03-03
老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)
下面小編就為大家?guī)?lái)一篇老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Android9?雙屏異顯實(shí)現(xiàn)方式思路
這篇文章主要為大家介紹了Android9?雙屏異顯實(shí)現(xiàn)方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

