Android开发 ---- 两分钟写一个录音演示软件

    Android自带的SoundRecoder软件写得很简单,就3个Java文件,最有特色的还算哪个指针了。这里并不是要介绍那个个指针的实现过程,其实也简单,就是一个算法,通过录音过程中获取的振幅来实现指针的偏移。
MediaRecorder.getMaxAmplitude(); // 得到录音时的最大振幅

    赶紧上代码吧,两分钟的时间马上就过了...界面设计很简单,3个按钮(开始录音,停止录音,播放录音)。

一、新建工程SoundRecoderDemo

二、main.xml(布局文件)

<?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:orientation="horizontal" >

        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="start" />

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="stop" />

        <Button
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="play" />

</LinearLayout>

三、SoundRecorderActivity(具体录音实现)

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SoundRecorderActivity extends Activity implements OnClickListener {

	private Button btnStart;
	private Button btnStop;
	private Button btnPlay;

	private MediaRecorder mMediaRecorder;
	private File recAudioFile;
	private MusicPlayer mPlayer;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		setupViews();
	}

	private void setupViews() {
		btnStart = (Button) findViewById(R.id.start);
		btnStop = (Button) findViewById(R.id.stop);
		btnPlay = (Button) findViewById(R.id.play);
		
		btnStart.setOnClickListener(this);
		btnStop.setOnClickListener(this);
		btnPlay.setOnClickListener(this);
		
		recAudioFile = new File("/mnt/sdcard", "new.amr");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.start:
			startRecorder();
			break;
		case R.id.stop:
			stopRecorder();
			break;
		case R.id.play:
			mPlayer = new MusicPlayer(SoundRecorderActivity.this);
			mPlayer.playMicFile(recAudioFile);
			break;
		default:
			break;
		}
	}

	private void startRecorder() {
		mMediaRecorder = new MediaRecorder();
		if (recAudioFile.exists()) {
			recAudioFile.delete();
		}

		mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
		mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
		mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
		mMediaRecorder.setOutputFile(recAudioFile.getAbsolutePath());
		try {
			mMediaRecorder.prepare();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		mMediaRecorder.start();
	}
	
	private void stopRecorder(){
		if (recAudioFile!=null) {
			mMediaRecorder.stop();
			mMediaRecorder.release();
		}
	}
}

四、播放类(MusicPlayer)

import java.io.File;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.util.Log;

public class MusicPlayer {
	private final static String TAG = "MusicPlayer";
	private static MediaPlayer mMediaPlayer;
	private Context mContext;
	
	public MusicPlayer(Context context){
		mContext = context;
	}
	
	public void playMicFile(File file){
		if (file!=null && file.exists()) {
			Uri uri = Uri.fromFile(file);
			mMediaPlayer = MediaPlayer.create(mContext, uri);
			mMediaPlayer.start(); 
			mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				
				public void onCompletion(MediaPlayer mp) {
					//TODO:finish 
					Log.i(TAG, "Finish");
				}
			});
		}
	}
	
	public void stopPlayer(){
		if(mMediaPlayer.isPlaying()){
			mMediaPlayer.stop();
			mMediaPlayer.release();
		}
	}
}

五、添加录音权限

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

完成,运行查看效果。代码很简单,没有注释,感兴趣的看API!!

Demo下载地址:http://download.csdn.net/detail/tangcheng_ok/3837435

原创文章,转载请注明出处:htttp://www.blog.csdn.net/tangcheng_ok


你可能感兴趣的:(android,File,layout,import,button,encoding)