读书人

MediaRecorder摄制音频

发布时间: 2012-07-18 12:05:40 作者: rapoo

MediaRecorder录制音频
一个录音的小程序!!

Main.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.recorder"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:label="@string/app_name"            android:name=".RecorderActivity" >            <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-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission></manifest>


RecorderActivity.java
package com.recorder;import java.io.File;import java.io.FilenameFilter;import java.io.IOException;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.Intent;import android.media.MediaRecorder;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.Toast;public class RecorderActivity extends Activity {/** Called when the activity is first created. */      private ListView mListView = null;      private Button btn_start = null;      private Button btn_stop = null;      private MediaRecorder mMediaRecorder = null;      private List<String> rec = new ArrayList<String>();// 存放录音文件      private File home = null;      private File path = null;      private String temp = "recaudio_";// 临时文件前缀        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          mListView = (ListView) this.findViewById(R.id.listView1);          btn_start = (Button) this.findViewById(R.id.start);          btn_stop = (Button) this.findViewById(R.id.stop);          // 是否存在SD卡          if (Environment.getExternalStorageState().equals(                  Environment.MEDIA_MOUNTED)) {              home = Environment.getExternalStorageDirectory();              MusicList();          } else {              Toast.makeText(this, "请先插入SD卡", Toast.LENGTH_LONG).show();              return;          }          btn_start.setOnClickListener(new Button.OnClickListener() {                public void onClick(View v) {                  // TODO Auto-generated method stub                  try {                      // 创建录音临时文件                      path = File.createTempFile(temp, ".amr", home);                      setTitle("=="+path.getAbsolutePath());                      mMediaRecorder = new MediaRecorder();                                            mMediaRecorder                              .setAudioSource(MediaRecorder.AudioSource.MIC);// 设置数据来源,麦克风                      mMediaRecorder                              .setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);// 设置格式                      mMediaRecorder                              .setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);// 设置编码                      mMediaRecorder.setOutputFile(path.getAbsolutePath());// 设置输出文件路径                      mMediaRecorder.prepare();                      mMediaRecorder.start();                  } catch (IOException e) {                      // TODO Auto-generated catch block                      e.printStackTrace();                  }              }            });          btn_stop.setOnClickListener(new Button.OnClickListener() {                public void onClick(View v) {                  // TODO Auto-generated method stub                  mMediaRecorder.stop();                  mMediaRecorder.release();                  mMediaRecorder = null;                  MusicList();              }            });          mListView.setOnItemClickListener(new OnItemClickListener(){                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,                      long arg3) {                  // TODO Auto-generated method stub                  String path = home+File.separator+rec.get(arg2);                  File f = new File(path);                  PlayMusic(f);              }                        });      }        /**      * 显示列表      */      public void MusicList() {          File[] f = home.listFiles(new MusicFilter());                    rec.clear();          for (int i = 0; i < f.length; i++) {              File file = f[i];              rec.add(file.getName());          }          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,                  android.R.layout.simple_list_item_1, rec);          mListView.setAdapter(adapter);      }        /**      * 播放录音文件      * @param file      */      public void PlayMusic(File file){          Intent intent = new Intent();          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);          intent.setAction(Intent.ACTION_VIEW);          intent.setDataAndType(Uri.fromFile(file), "audio");          this.startActivity(intent);      }      class MusicFilter implements FilenameFilter {            public boolean accept(File dir, String filename) {              // TODO Auto-generated method stub              return (filename.endsWith(".amr"));          }        } }

读书人网 >移动开发

热点推荐