Android开发 ---- 两分钟写一个录音演示软件
Android自带的SoundRecoder软件写得很简单,就3个Java文件,最有特色的还算哪个指针了。这里并不是要介绍那个个指针的实现过程,其实也简单,就是一个算法,通过录音过程中获取的振幅来实现指针的偏移。
<span style="font-size:16px;">MediaRecorder.getMaxAmplitude(); // 得到录音时的最大振幅</span>
赶紧上代码吧,两分钟的时间马上就过了...界面设计很简单,3个按钮(开始录音,停止录音,播放录音)。
一、新建工程SoundRecoderDemo
二、main.xml(布局文件)
<span style="font-size:16px;"><?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></span>
三、SoundRecorderActivity(具体录音实现)
<span style="font-size:16px;">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.getAbsolut
补充:移动开发 , Android ,