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


package com.Aina.Android;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.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class Test 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_";// 临时文件前缀@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mListView = (ListView) this.findViewById(R.id.ListView01);btn_start = (Button) this.findViewById(R.id.Button01);btn_stop = (Button) this.findViewById(R.id.Button02);// 是否存在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 stubtry {// 创建录音临时文件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 blocke.printStackTrace();}}});btn_stop.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubmMediaRecorder.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 stubString 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 stubreturn (filename.endsWith(".amr"));}}}

更多相关文章

  1. Android Studio 指定DEBUG和RELEASE版本打包时的APK文件名
  2. Android中使用 MediaExtractor 和 MediaMuxer解析、封装 mp4 文
  3. android:shape的使用 (android用xml文件生成图像控件)
  4. android 自定义View 在布局文件中写法
  5. Android上传下载文件(图片)
  6. android 录像/打开video文件
  7. (原)Eclipse的java中文件读写
  8. Android File文件操作
  9. Android Button Maker(在线生成android shape xml文件的工具),真方

随机推荐

  1. appium測試中验证toast的正确性
  2. Android压缩
  3. 代码中如何设置TextView为不可见
  4. Android判断当前线程是否是主线程的方法
  5. Android(安卓)OpenGL ES(八)----纹理编程
  6. android-passwordsafe - Android Passwor
  7. Android判断横屏竖屏代码
  8. Android Studio v1.0 项目无法运行
  9. Android Unable to find instrumentation
  10. Android使用Aidl实现跨进程通信