<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ouling.ex_recording"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

     <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Ex_recordingActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ImageView
        android:id="@+id/pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/android256" />

    <Button
        android:id="@+id/record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="开始学习说话" />

</LinearLayout>



package com.ouling.ex_recording;

//录音效果

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder.AudioSource;
import android.media.MediaRecorder.OutputFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class Ex_recordingActivity extends Activity {

	MediaRecorder mr;//录音
	MediaPlayer mplayer;//播放
	final String filepath = "/sdcard/androidRecording.amr";
	Button btn_record;
	ImageView imgview;
	boolean is_recording=false;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		btn_record=(Button)findViewById(R.id.record);
		imgview=(ImageView)findViewById(R.id.pic);
		
		btn_record.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {
				case R.id.record:
					if (is_recording) {
						btn_record.setText("播放中...");
						mrstop();
						vplay(filepath);
						is_recording=false;
					}else {
						btn_record.setText("学习中。。。");
						mrstart();
						is_recording=true;
					}
					
					break;

				default:
					break;
				}
			}
		});
	}
	
	

	// 开发录音
	private boolean mrstart() {
		// TODO Auto-generated method stub
		mr = new MediaRecorder();
		mr.setAudioSource(AudioSource.MIC);
		// 设置音源,这里是来自麦克风
		mr.setOutputFormat(OutputFormat.RAW_AMR);
		// 输出格式
		mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		// 编码
		mr.setOutputFile(filepath);
		// 输出文件路径
		try {
			mr.prepare();
			// 做些准备工作
			mr.start();
			// 开始
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// 停止录音
	private void mrstop() {
		// TODO Auto-generated method stub
		if (mr != null) {
			mr.stop();// 停止
			mr.release();// 释放
		}
	}
	
	//播放录音
	void vplay(String path) {
		try {
			mplayer= new MediaPlayer();
			mplayer.reset();
			mplayer.setDataSource(path);
			mplayer.prepare();// 准备同步
			mplayer.start();
			//播放完成
			mplayer.setOnCompletionListener(new OnCompletionListener() {
				@Override
				public void onCompletion(MediaPlayer arg0) {
					// TODO Auto-generated method stub
					mplayer.release();
					btn_record.setText("开始学习说话");
					is_recording=false;
					
				}
			});
		} catch (Exception e) {
			System.out.println(e.toString());
			if (mplayer!=null) {
				mplayer.release();				
			}
			btn_record.setText("开始学习说话");
			is_recording=false;
			Toast.makeText(Ex_recordingActivity.this, "无法播放", 1000).show();
		}
	}

	// 当用户返回时
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mplayer!=null) {
				mplayer.stop();
				mplayer.release();				
			}
			if (mr!=null ) {
				mr.stop();
				mr.release();
			}
			this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}