现在的位置: 首页 > 综合 > 正文

Android 录制音频示例

2013年12月08日 ⁄ 综合 ⁄ 共 3606字 ⁄ 字号 评论关闭
1)布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/start_record"
   android:text="Start Audio Recording"
    />
<Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
   android:id="@+id/stop_record"
    android:text="Stop Audio Recording"
    />
</LinearLayout>

2)AudioRecordActivity.java
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AudioRecordActivity extends Activity {
public static final String TAG = AudioRecordActivity.class.getSimpleName();
private MediaRecorder mediaRecord = null;
private Button btnStart, btnStop;
private File storeFile = null;
private MyOnClickListener listener = null;
private static final int TAG_START = 0;
private static final int TAG_STOP = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_record);
mediaRecord = new MediaRecorder();
listener = new MyOnClickListener();
btnStart = (Button)findViewById(R.id.start_record);
btnStart.setTag(TAG_START);
btnStart.setOnClickListener(listener);
btnStop = (Button)findViewById(R.id.stop_record);
btnStop.setTag(TAG_STOP);
btnStop.setOnClickListener(listener);
btnStart.setEnabled(true);
btnStop.setEnabled(false);
}
private void onStartRecord() {
btnStart.setEnabled(false);
btnStop.setEnabled(true);
btnStop.requestFocus();
try {
startRecord();
} catch (IllegalStateException e) {
Log.e(TAG, e.toString());
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
private void startRecord() throws IllegalStateException, IOException {
mediaRecord.setAudioSource(MediaRecorder.AudioSource.MIC);//设置音频源
mediaRecord.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置音频输出格式
mediaRecord.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置音频编码
if(storeFile == null){
File sampleDir = Environment.getExternalStorageDirectory();
try {
storeFile = File.createTempFile("freedom", ".3gp", sampleDir);
} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
}
mediaRecord.setOutputFile(storeFile.getAbsolutePath());
mediaRecord.prepare();
mediaRecord.start();
}
private void onStopRecord() {
btnStart.setEnabled(true);
btnStop.setEnabled(false);
btnStart.requestFocus();
stopRecord();
}
private void stopRecord() {
mediaRecord.stop();
mediaRecord.release();
processAudioFile();
}
private void processAudioFile() {
ContentValues cv = new ContentValues();
long currentTime = System.currentTimeMillis();
cv.put(MediaStore.Audio.Media.TITLE, "audio" + storeFile.getName());
cv.put(MediaStore.Audio.Media.DATE_ADDED, (int)currentTime/1000);
cv.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gp");
cv.put(MediaStore.Audio.Media.DATA, storeFile.getAbsolutePath());
Uri uri = getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cv);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
}
class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View view) {
switch ((Integer)view.getTag()) {
case TAG_START:
onStartRecord();
break;
case TAG_STOP:
onStopRecord();
break;
default:
break;
}
}
}
}
注意:需要在AndroidManifest.xml中加入以下权限
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

抱歉!评论已关闭.