多媒体 使用MediaRecorder录制音频

来源:互联网 发布:软件激活码商城 编辑:程序博客网 时间:2024/05/18 01:53
多媒体 使用MediaRecorder录制音频android提供了MediaRecorder类。使用MediaRecorder录制音频的过程很简单,步骤如下:1. 创建MediaRecorder对象2. 调用MediaRecorder对象的setAudioSource()方法设置声音来源,一般传入MediaRecorder。AduioSource.MIC 参数指定mic的声音。3. 调用MediaRecorder对象的setOutputFormat()设置所录制的音频文件格式。4. 调用MediaRecorder对象的setAudioEncoder(),setAduioEncodingBitRate(int bitRate),setAudioSamplingRate(int samplingRate)所录制编码格式 编码位率,采样率等。5. 调用MediaRecorder的setOutputFile(String path)方法设置录制的音频文件的保存位置。6. 调用MeidaRecorder的prepare()方法准备录制。7. 调用MediaRecorder的start方法开始录制。8. 录制完成,调用MediaRecorder对象的stop()方法停止录制,并调用release()方法释放资源。package com.example.recordersoundtest;import java.io.File;import java.io.IOException;import android.support.v7.app.ActionBarActivity;import android.media.MediaRecorder;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore.Audio.Media;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.Toast;public class MainActivity extends ActionBarActivity implements OnClickListener {// buttonImageButton record, stop;// system audio fileFile soundFile;MediaRecorder mRecorder;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);record = (ImageButton) findViewById(R.id.record);stop = (ImageButton) findViewById(R.id.stop);record.setOnClickListener(this);stop.setOnClickListener(this);}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubtry {if (soundFile != null && soundFile.exists()) {mRecorder.stop();mRecorder.release();mRecorder = null;}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}super.onDestroy();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.record:try {if (!Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {Toast.makeText(this,"sdcard is not insert! pls insert in !", 5000).show();return;}try {soundFile = new File(Environment.getExternalStorageDirectory().getCanonicalFile()+ "/sound.amr");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}mRecorder = new MediaRecorder();mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);mRecorder.setOutputFile(soundFile.getAbsolutePath());try {mRecorder.prepare();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}mRecorder.start();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}break;case R.id.stop:try {if (soundFile != null && soundFile.exists()) {mRecorder.stop();mRecorder.release();mRecorder = null;}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}break;default:break;}}}注意申请相关权限,<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.recordersoundtest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" 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></manifest>
原创粉丝点击