一个录音的小程序!!
Main.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.recorder" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".RecorderActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> </manifest>
RecorderActivity.java
package com.recorder; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.util.ArrayList; import java.util.List; 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.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; public class RecorderActivity extends Activity { /** Called when the activity is first created. */ private ListView mListView = null; private Button btn_start = null; private Button btn_stop = null; private MediaRecorder mMediaRecorder = null; private List<String> rec = new ArrayList<String>();// 存放录音文件 private File home = null; private File path = null; private String temp = "recaudio_";// 临时文件前缀 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mListView = (ListView) this.findViewById(R.id.listView1); btn_start = (Button) this.findViewById(R.id.start); btn_stop = (Button) this.findViewById(R.id.stop); // 是否存在SD卡 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { home = Environment.getExternalStorageDirectory(); MusicList(); } else { Toast.makeText(this, "请先插入SD卡", Toast.LENGTH_LONG).show(); return; } btn_start.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub try { // 创建录音临时文件 path = File.createTempFile(temp, ".amr", home); setTitle("=="+path.getAbsolutePath()); mMediaRecorder = new MediaRecorder(); mMediaRecorder .setAudioSource(MediaRecorder.AudioSource.MIC);// 设置数据来源,麦克风 mMediaRecorder .setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);// 设置格式 mMediaRecorder .setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);// 设置编码 mMediaRecorder.setOutputFile(path.getAbsolutePath());// 设置输出文件路径 mMediaRecorder.prepare(); mMediaRecorder.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); btn_stop.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub mMediaRecorder.stop(); mMediaRecorder.release(); mMediaRecorder = null; MusicList(); } }); mListView.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub String path = home+File.separator+rec.get(arg2); File f = new File(path); PlayMusic(f); } }); } /** * 显示列表 */ public void MusicList() { File[] f = home.listFiles(new MusicFilter()); rec.clear(); for (int i = 0; i < f.length; i++) { File file = f[i]; rec.add(file.getName()); } ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, rec); mListView.setAdapter(adapter); } /** * 播放录音文件 * @param file */ public void PlayMusic(File file){ Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "audio"); this.startActivity(intent); } class MusicFilter implements FilenameFilter { public boolean accept(File dir, String filename) { // TODO Auto-generated method stub return (filename.endsWith(".amr")); } } }
/** * 播放录音文件 * @param file */ public void PlayMusic(File file){ Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "audio"); this.startActivity(intent); }
private final State mThreadLocker = new State();
private UpdateThread updateThread;
private DrawThread drawThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
updateThread = new UpdateThread();
updateThread.start();
drawThread = new DrawThread();
drawThread.start();
}
protected void onUpdate() throws InterruptedException {
final State threadLocker = this.mThreadLocker;
threadLocker.waitUntilCanUpdate();
System.out.println("Update");
threadLocker.notifyCanDraw();
}
public void onDrawFrame() throws InterruptedException {
final State threadLocker = this.mThreadLocker;
threadLocker.waitUntilCanDraw();
System.out.println("Draw");
threadLocker.notifyCanUpdate();
}
private class UpdateThread extends Thread {
private boolean flag; // 线程执行标志位
public UpdateThread() {
super("UpdateThread");
this.flag = true;
}
@Override
public void run() {
try {
while (flag) {
onUpdate();
}
} catch (final InterruptedException e) {
this.interrupt();
e.printStackTrace();
}
}
/*
* 停止线程
*/
public void requestStop() {
flag = false;
}
}
private class DrawThread extends Thread {
private boolean flag; // 线程执行标志位
public DrawThread() {
super("DrawThread");
this.flag = true;
}
@Override
public void run() {
try {
while (flag) {
onDrawFrame();
}
} catch (final InterruptedException e) {
this.interrupt();
e.printStackTrace();
}
}
/*
* 停止线程
*/
public void requestStop() {
flag = false;
}
}
private static class State {
boolean mDrawing = false;
public synchronized void notifyCanDraw() {
this.mDrawing = true;
this.notifyAll();
}
public synchronized void notifyCanUpdate() {
this.mDrawing = false;
this.notifyAll();
}
public synchronized void waitUntilCanDraw() throws InterruptedException {
while (this.mDrawing == false) {
this.wait();
}
}
public synchronized void waitUntilCanUpdate()
throws InterruptedException {
while (this.mDrawing == true) {
this.wait();
}
}
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
updateThread.requestStop();
drawThread.requestStop();
}
@Override
public String toString() {
return "MultiThreadAndSync";
}