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.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();
}
}
}</span>
四、播放類(MusicPlayer)
<span style="font-size:16px;">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();
}
}
}
</span>
五、添加錄音許可權
<span style="font-size:16px;"><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" /></span>
完成,運行查看效果。代碼很簡單,沒有注釋,感興趣的看API!!
原創文章,轉載請註明出處:htttp://www.blog.csdn.net/tangcheng_ok