MediaRecorder一般录制视频,只能是amr格式的

public class AudioListView extends ListActivity implements OnClickListener {
	private Button play_btn;// 播放
	private Button record_btn;// 录音
	private Button pause_btn;// 暂停
	private Button del_btn;// 删除

	private TextView list_id_txt;

	private File fileName;/* 录制的音频文件 */
	private File filePath;
	String SDPATH = null;
	private static final String AUDIO_DIR = Environment
			.getExternalStorageDirectory().getAbsolutePath()
			+ File.separator + "mobileoa" + File.separator + "audio";// 录音目录

	private MediaRecorder mMediaRecorder;/* MediaRecorder对象 */
	private List mMusicList = new ArrayList();/* 录音文件列表 */
	private String tempName = "";/* 零时文件的前缀 */
	private int filePosition = 0;// 文件ID

	/* 顶部菜单 */
	Button tBack_btn;// 返回按钮
	Button tMenu_btn;// 菜单按钮
	TextView title_txt;// 标题
	RelativeLayout audioLayout;
	RelativeLayout mainFrame;
	GlobalVariable gv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		gv = (GlobalVariable) getApplication();
		gv.getActivityList().add(this);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏注意,这个设置必须放在设置布局前面,不然会报错
		/* 渲染主窗体 */
		LayoutInflater inflater = LayoutInflater.from(AudioListView.this);
		mainFrame = (RelativeLayout) inflater.inflate(
				R.layout.audiolistview, null);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

		addContentView(mainFrame, params);
		mainFrame.setBackgroundResource(gv.getBackground());
		/* 主窗体中添加公共的顶部菜单 */
		RelativeLayout topmenu = (RelativeLayout) inflater.inflate(
				R.layout.topmenu, null);

		mainFrame.addView(topmenu, params);
		this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title);


		/*----------------顶部菜单的处理----------------------------- */
		// 获得标题对象
		title_txt = (TextView) findViewById(R.id.title_text);
		// 设置标题的内容
		title_txt.setText("录音");

		tBack_btn = (Button) findViewById(R.id.topback_btn);// 获得返回按钮对象
		tBack_btn.setOnClickListener(new OnClickListener() {// 注册返回按钮事件

					@Override
					public void onClick(View v) {
						AudioListView.this.finish();

					}
				});
		tMenu_btn = (Button) findViewById(R.id.topemenu_btn);// 获得菜单按钮对象
		tMenu_btn.setVisibility(View.INVISIBLE);
		checkState();
		musicList();/* 更新所有录音文件到List中,这边的方法只被执行一次 */
		findView();
		setListener();
	}

	// 检查是否有内存卡
	private void checkState() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			File file = new File(AUDIO_DIR);
			if (!file.isDirectory()) {
				file.mkdirs();
			}
			filePath = file;
		} else {
			Toast.makeText(AudioListView.this, "SD卡不存在!无法保存相片", 3000).show();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		clearList();
		musicList();/* 更新所有录音文件到List中 */
	}

	@Override
	protected void onPostResume() {
		super.onPostResume();
		clearList();
		musicList();/* 更新所有录音文件到List中 */
	}

	/**
	 * 控件实例化
	 */
	private void findView() {
		play_btn = find(play_btn, R.id.play_btn);
		record_btn = find(record_btn, R.id.record_btn);
		pause_btn = find(pause_btn, R.id.pause_btn);
		del_btn = find(del_btn, R.id.del_btn);
		list_id_txt = (TextView) findViewById(R.id.list_id_txt);
	}

	/**
	 * Button实例化方法
	 */
	public Button find(Button button, int id) {
		button = (Button) findViewById(id);
		return button;
	}

	/**
	 * 注册监听事件
	 */
	private void setListener() {
		play_btn.setOnClickListener(this);
		record_btn.setOnClickListener(this);
		pause_btn.setOnClickListener(this);
		del_btn.setOnClickListener(this);
		// back_btn.setOnClickListener(new BackOnClickListener());
	}

	/**
	 * 返回按钮的监听事件
	 */
	class BackOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Service.TurnMainView(AudioListView.this, MainView.class, MainView2.class, getString(R.string.dbname));
			AudioListView.this.finish();
		}

	}

	/**
	 * 播放录音
	 */
	private void playMusic(File file) {
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(android.content.Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(file), "audio");/* 设置文件类型 */
		startActivity(intent);
	}

	/**
	 * 根据位置获取文件
	 * 
	 * @param position
	 */
	public File getFile(int position) {
		File playfile = new File(filePath.getAbsolutePath() + File.separator
				+ mMusicList.get(position));
		return playfile;
	}

	/**
	 * 列表项监听事件
	 */
	protected void onListItemClick(ListView l, View v, int position, long id) {
		File playfile = new File(filePath.getAbsolutePath() + File.separator
				+ mMusicList.get(position));
		list_id_txt.setText(playfile.getName());// 设置示当前文件名
		filePosition = position;
	}

	/**
	 * 添加播放列表
	 */
	public void musicList() {
		// 取得指定位置的文件设置显示到播放列表
		File home = filePath;
		if (home.listFiles(new MusicFilter()).length > 0) {
			for (File file : home.listFiles(new MusicFilter())) {
				mMusicList.add(file.getName());
			}
			ArrayAdapter musicList = new ArrayAdapter(
					AudioListView.this, R.layout.listaudio, mMusicList);
			setListAdapter(musicList);

		}
	}

	// 先清空ListView原来的数据
	public void clearList() {
		mMusicList.clear();
		ArrayAdapter musicList = new ArrayAdapter(
				AudioListView.this, R.layout.listaudio, mMusicList);
		setListAdapter(musicList);
	}

	/* 过滤文件类型 */
	class MusicFilter implements FilenameFilter {
		public boolean accept(File dir, String name) {
			return (name.endsWith(".amr"));
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		String s = list_id_txt.getText().toString();
		switch (id) {
		case R.id.record_btn:// 录音
			pause_btn.setEnabled(true);
			record_btn.setEnabled(false);
			Toast.makeText(AudioListView.this, "开始录音...", 1000).show();
			record_btn.setText("录音中...");
			try {

				tempName = new Universal().getTimeStr() + "_";// 获取时间
				fileName = File.createTempFile(tempName, ".amr", filePath);/* 创建录音文件 */

				mMediaRecorder = new MediaRecorder();/* 实例化MediaRecorder对象 */
				mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);/* 设置麦克风 */
				mMediaRecorder
						.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);/* 设置输出文件的格式 */
				mMediaRecorder
						.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);/* 设置音频文件的编码 */
				mMediaRecorder.setOutputFile(fileName.getAbsolutePath());/* 设置输出文件的路径 */
				mMediaRecorder.prepare();/* 准备 */
				mMediaRecorder.start();/* 开始 */
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		case R.id.play_btn:// 播放
			if (s.equals("")) {
				Toast.makeText(AudioListView.this, "请选择一条要播放的录音!", 1000).show();
			} else {
				playMusic(getFile(filePosition));
			}

			break;
		case R.id.pause_btn:// 停止
			record_btn.setText("录音");
			if (fileName != null) {
				mMediaRecorder.stop();/* 停止录音 */
				mMusicList.add(fileName.getName());/* 将录音文件添加到List中 */
				ArrayAdapter musicList = new ArrayAdapter(
						AudioListView.this, R.layout.listaudio, mMusicList);
				setListAdapter(musicList);
				mMediaRecorder.release();/* 释放MediaRecorder */
				mMediaRecorder = null;
			}
			pause_btn.setEnabled(false);
			record_btn.setEnabled(true);
			new AlertDialog.Builder(AudioListView.this)
			.setTitle("提示")
			.setMessage("录音完成,是否添加到邮件附件?")
			.setPositiveButton("添加", new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					FileChooser.fileName = fileName.getName();
					FileChooser.filePath = fileName.getAbsolutePath();
					finish();
				}
			})
			.setNegativeButton("取消", null).create().show();
			break;
		case R.id.del_btn:// 删除
			if (s.equals("")) {
				Toast.makeText(AudioListView.this, "请选择一条要删除的记录!", 1000).show();
			} else {
				new AlertDialog.Builder(AudioListView.this).setMessage(
						"确定要删除吗?").setPositiveButton("是",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								File file = new File(filePath.getAbsolutePath()
										+ File.separator
										+ mMusicList.get(filePosition));
								file.delete();
								clearList();// 先清空原数据
								musicList();// 重新读取数据
								list_id_txt.setText("");
							}
						}).setNegativeButton("否",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								// finish();
							}
						}).show();
			}
			break;

		default:
			break;
		}
	}


}

 

你可能感兴趣的:(多媒体,移动开发)