Android录制音频

我们通常可以通过以下步骤在Android中录制音频或视频

(1)   创建android.media.MediaRecorder的一个实例。

(2)   创建android.content.ContentValues的一个实例,并添加TITLE、TIMESTAMP及重要的MIME_TYPE等属性。

(3)   使用android.content.ContentResolver为数据创建一个目标文件路径。

(4)   要在视图界面上设置预览显示,可以使用MediaRecorder.setPreviewDisplay()方法。

(5)   使用MediaRecorder.setAudioSource()设置音频来源。

(6)   使用MediaRecorder.setOutputFormat()设置输出文件格式。

(7)   使用MediaRecorder.setAudioEncoder()设置音频编码。

(8)   使用prepare()和start()准备和开始录制。

(9)   使用stop()和release()正常停止和清除录制过程。

代码如下:

------------------------RecordingActivity --------------------------

package cn.com.pan;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentResolver;
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.provider.MediaStore.MediaColumns;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class RecordingActivity extends Activity {
 MediaRecorder mRecorder;
 File mSampleFile = null;
 static final String SAMPLE_PREFIX = "recording";
 static final String SAMPLE_EXTENSION = ".mp3";
 private static final String TAG = "RecordingActivity";
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  this.mRecorder = new MediaRecorder();
  Button startRecording = (Button) this.findViewById(R.id.startRecording);
  Button stopRecording = (Button) this.findViewById(R.id.stopRecording);
  startRecording.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    startRecording();
   }
  });
  stopRecording.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    stopRecording();
    addToDB();
   }
  });
 }
 protected void addToDB() {
  ContentValues values = new ContentValues(3);
  long current = System.currentTimeMillis();
  values.put(MediaColumns.TITLE, "test_audio");
  values.put(MediaColumns.DATE_ADDED, (int) (current / 1000));
  values.put(MediaColumns.MIME_TYPE, "audio/mp3");
  values.put(MediaColumns.DATA, mSampleFile.getAbsolutePath());
  ContentResolver contentResolver = getContentResolver();
  Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  Uri newUri = contentResolver.insert(base, values);
  sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
 }
 protected void startRecording() {
  this.mRecorder = new MediaRecorder();
  this.mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  this.mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  this.mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  this.mRecorder.setOutputFile(this.mSampleFile.getAbsolutePath());
  try {
   this.mRecorder.prepare();
  } catch (IllegalStateException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  } catch (IOException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  this.mRecorder.start();
  if (this.mSampleFile == null) {
   File sampleDir = Environment.getExternalStorageDirectory();
   try {
    this.mSampleFile = File.createTempFile(
      RecordingActivity.SAMPLE_PREFIX,
      RecordingActivity.SAMPLE_EXTENSION, sampleDir);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    Log.e(TAG, "sdcard access error");
    return;
   }
  }
 }
 protected void stopRecording() {
  this.mRecorder.stop();
  this.mRecorder.release();
 }
}

--------------------------------main.xml -------------------------------------

<?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:id="@+id/startRecording" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="start"></Button>
 <Button android:id="@+id/stopRecording" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="stop"></Button>
</LinearLayout>

---------------------------------AndroidManifest.xml---------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="cn.com.pan" android:versionCode="1" android:versionName="1.0">
 <application android:icon="@drawable/icon" android:label="@string/app_name">
  <activity android:name=".RecordingActivity" android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
 </application>
 <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
 <uses-sdk android:minSdkVersion="3" />
</manifest>

如果在计算机的音频系统中播放音乐,Android Emulator将选取该音乐并直接录制到音频缓冲区中(并不是从麦克风进行实际录制)。然后可以轻松测试该音频文件,方法是打开Android Music Player并选择Playlist>Recently Added。播放器就会播放刚录制的音频文件了。

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值