Android 录制音频示例

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"/>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值