Android学习11-----多媒体技术(5) 媒体录制

       在Android中通过android.media.MeidaRecoder类实现音频和视频文件的录制操作。

 

Android学习11-----多媒体技术(5) 媒体录制

 

 

MediaRecorder 的生命周期包括以下几种状态:

1、 Initial 状态 :当用户通过 MediaRecorder 类的构造方法实例化 MediaRecorder 类对象时将处于初始化状态,即便此时没有任何操作, MediaRecorder 也会占用系统资源,而所有的状态都可以通过 reset() 方法返回到此状态。

2、 Initialized 状态 :当用户使用 setAudioSource() setVideoSource() 方法后将进入音频录制或视频录制状态,并可以指定一些音频或视频的文件属性,设置完成之后将进入 DataSourceConfigured 状态。

3、 Prepared 状态 :就绪状态,当用户使用 MediaRecorder 类中的 prepare() 方法时进入就绪状态,表示录制前的状态已经准备就绪。

4、 Recoding 状态 :但用户使用 MediaRecorder 类中的 start() 方法时,将进入录制状态,并且一直持续到录音或录像操作完毕。

 

下面使用 MediaRecorder 完成一个音频录制以及音频文件列表

列表显示布局 --recordaudios.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TableRow>
		<ImageView
			android:id="@+id/icon" 
			android:layout_width="fill_parent"
			android:layout_height="wrap_content" 
			android:src="@drawable/audio_icon" />
		<TextView
			android:id="@+id/filename"
			android:textSize="16px"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
	</TableRow>
</TableLayout>

 

audiomain.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">
	<LinearLayout 
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="horizontal" 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<ImageButton
			android:id="@+id/record"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:src="@drawable/record" />
		<ImageButton
			android:id="@+id/stop"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:src="@drawable/stop" />
	</LinearLayout>
	<TextView 
		android:id="@+id/info"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="文字提示信息..." />
	<ListView
		android:id="@+id/reclist"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
</LinearLayout>

 

AudioRecorderActivity.java

package com.iflytek.demo;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class AudioRecorderActivity extends Activity {
	private ImageButton recordBtn = null;
	private ImageButton stopBtn = null;
	private TextView infoTxt = null;// 提示信息
	private ListView reclist = null;// 定义列表视图
	private SimpleAdapter recordSimpleAdapter = null;// 用于定义ListView组件的适配器
	private MediaRecorder mediaRecorder = null;// MediaRecorder对象
	private boolean sdcardExists = false; // 判断sd卡是否存在
	private File recordAudioSaveFileDir = null; // 保存所有音频文件的文件夹
	private File recordAudioSaveFile = null; // 每次保存音频文件的名称
	private String recordAudioSaveFileName = null; // 每次保存音频文件的名称
	private String recDir = "xdwangAudio"; // 文件保存的目录路径
	private boolean isRecord = false; // 判断是否正在录音
	private List<Map<String, Object>> recordFiles = null;// 保存所有的List数据

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.audiomain);
		this.recordBtn = (ImageButton) super.findViewById(R.id.record);
		this.stopBtn = (ImageButton) super.findViewById(R.id.stop);
		this.infoTxt = (TextView) super.findViewById(R.id.info);
		this.reclist = (ListView) super.findViewById(R.id.reclist);
		// 如果存在则将状态给了sdcardExists属性
		if ((this.sdcardExists = Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED))) { // 判断sd卡是否存在
			this.recordAudioSaveFileDir = new File(Environment
					.getExternalStorageDirectory().toString()
					+ File.separator
					+ AudioRecorderActivity.this.recDir + File.separator);// 保存录音目录
			if (!this.recordAudioSaveFileDir.exists()) { // 文件夹不存在
				this.recordAudioSaveFileDir.mkdirs(); // 创建新的文件夹
			}
		}
		this.stopBtn.setEnabled(false); // 暂停按钮暂时不可用
		this.getRecordFiles();// 取得全部的文件列表
		this.recordBtn.setOnClickListener(new RecordOnClickListenerImpl());
		this.stopBtn.setOnClickListener(new StopOnClickListenerImpl());
		this.reclist.setOnItemClickListener(new OnItemClickListenerImpl());
	}

	/**
	 * 
	 * @author xdwang
	 * 
	 * @create 2012-11-15 下午8:10:37
	 * 
	 * @email:[email protected]
	 * 
	 * @description 音频录制,默认格式为.3gp
	 * 
	 */
	private class RecordOnClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View v) {
			if (AudioRecorderActivity.this.sdcardExists) { // 如果sd卡存在
				AudioRecorderActivity.this.recordAudioSaveFileName = AudioRecorderActivity.this.recordAudioSaveFileDir
						.toString()
						+ File.separator
						+ "NoteRecord_"
						+ System.currentTimeMillis() + ".3gp"; // 每次的录音文件名称都不一样
				AudioRecorderActivity.this.recordAudioSaveFile = new File(
						AudioRecorderActivity.this.recordAudioSaveFileName);
				AudioRecorderActivity.this.mediaRecorder = new MediaRecorder(); // 实例化对象
				// 在进行录制之前必须配置若干个参数
				AudioRecorderActivity.this.mediaRecorder
						.setAudioSource(MediaRecorder.AudioSource.MIC); // 音频来源是MIC
				AudioRecorderActivity.this.mediaRecorder
						.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// 定义输出格式
				AudioRecorderActivity.this.mediaRecorder
						.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); // 定义音频编码
				AudioRecorderActivity.this.mediaRecorder
						.setOutputFile(AudioRecorderActivity.this.recordAudioSaveFileName);// 定义输出文件
				try { // 进入到就绪状态
					AudioRecorderActivity.this.mediaRecorder.prepare();// 准备录音
				} catch (Exception e) {
					// Log.i("VideoRecorder", e.toString()) ;
				}
				AudioRecorderActivity.this.mediaRecorder.start(); // 开始录音
				AudioRecorderActivity.this.infoTxt.setText("正在录音中...");
				AudioRecorderActivity.this.stopBtn.setEnabled(true); // 停止录音按钮可以使用了
				AudioRecorderActivity.this.recordBtn.setEnabled(false);// 录音按钮禁用
				AudioRecorderActivity.this.isRecord = true; // 正在录音
			}
		}
	}

	/**
	 * 
	 * @author xdwang
	 * 
	 * @create 2012-11-15 下午8:48:15
	 * 
	 * @email:[email protected]
	 * 
	 * @description 暂停录音
	 * 
	 */
	private class StopOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (AudioRecorderActivity.this.isRecord) { // 正在录音
				AudioRecorderActivity.this.mediaRecorder.stop(); // 停止
				AudioRecorderActivity.this.mediaRecorder.release(); // 释放资源
				AudioRecorderActivity.this.recordBtn.setEnabled(true);// 录音按钮启用
				AudioRecorderActivity.this.stopBtn.setEnabled(false);// 停止录音按钮禁用
				AudioRecorderActivity.this.infoTxt.setText("录音结束,文件路径为:"
						+ AudioRecorderActivity.this.recordAudioSaveFileName);
				AudioRecorderActivity.this.getRecordFiles();// 重新加载列表
			}
		}
	}

	/**
	 * @descrption 取得全部录音文件
	 * @author xdwang
	 * @create 2012-11-15下午7:39:04
	 */
	private void getRecordFiles() { // 将一个文件夹之中的全部文件列出
		this.recordFiles = new ArrayList<Map<String, Object>>();
		if (this.sdcardExists) { // 如果sd卡存在
			File files[] = this.recordAudioSaveFileDir.listFiles(); // 列出目录中的文件
			for (int x = 0; x < files.length; x++) {
				Map<String, Object> fileInfo = new HashMap<String, Object>();
				fileInfo.put("filename", files[x].getName());// 增加显示内容
				this.recordFiles.add(fileInfo);// 保存数据
			}
			this.recordSimpleAdapter = new SimpleAdapter(this,
					this.recordFiles, R.layout.recordaudios,
					new String[] { "filename" }, new int[] { R.id.filename });// 实例化适配器
			this.reclist.setAdapter(this.recordSimpleAdapter);// 定义列表视图
		}
	}

	/**
	 * 
	 * @author xdwang
	 * 
	 * @create 2012-11-15 下午9:02:33
	 * 
	 * @email:[email protected]
	 * 
	 * @description 指定音频文件播放的操作
	 * 
	 */
	private class OnItemClickListenerImpl implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {// 选项单击
			if (AudioRecorderActivity.this.recordSimpleAdapter
					.getItem(position) instanceof Map) {// 判断是否是Map实例
				Map<?, ?> map = (Map<?, ?>) AudioRecorderActivity.this.recordSimpleAdapter
						.getItem(position);// 取出指定位置的内容
				Uri uri = Uri.fromFile(new File(
						AudioRecorderActivity.this.recordAudioSaveFileDir
								.toString()
								+ File.separator
								+ map.get("filename")));// 定义操作的Uri
				Intent intent = new Intent(Intent.ACTION_VIEW);// 指定Action
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 增加标记
				intent.setDataAndType(uri, "audio/mp3");// 设置数据播放的MIME
				AudioRecorderActivity.this.startActivity(intent);// 启动Activity
			}
		}
	}
}
 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iflytek.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AudioRecorderActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
 

录制视频与录制音频文件的功能类似,唯一的区别就是在视频录制中需要定义一个额外的SurfaceView组件,以捕获摄像头采集到的数据。
范例:视频录制、列表显示、视频播放
recordvideo.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">
	<LinearLayout 
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="horizontal" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
		<ImageButton
			android:id="@+id/record"
			android:layout_width="wrap_content"
			android:layout_height="fill_parent" 
			android:src="@drawable/record"/>
		<ImageButton
			android:id="@+id/stop"
			android:layout_width="wrap_content"
			android:layout_height="fill_parent" 
			android:src="@drawable/stop"/>
		<ImageButton
			android:id="@+id/browser"
			android:layout_width="wrap_content"
			android:layout_height="fill_parent" 
			android:src="@drawable/browser"/>
	</LinearLayout>
	<TextView 
		android:id="@+id/info"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="文字提示信息" />
	<SurfaceView
		android:id="@+id/surface"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent" />
</LinearLayout>
 

VideoRecorderActivity.java

package com.iflytek.demo;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

public class VideoRecorderActivity extends Activity {

	private ImageButton recordBtn = null;
	private ImageButton stopBtn = null;
	private ImageButton browserBtn = null;
	private TextView infoTxt = null;
	private MediaRecorder mediaRecorder = null;
	private File recordVideoSaveFile = null;// 文件保存目录路径
	private File recordVideoSaveFileDir = null;// 文件保存目录
	private String recordVideoSaveFileName = null;// 音频文件保存名称
	private String recDir = "xdwangvideo";// 保存目录
	private boolean sdcardExists = false; // SD卡存在的标记
	private boolean isRecord = false;// 判断是否正在录音
	private SurfaceView surfaceView = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题
		super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
		super.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 高亮的风格显示
		super.setContentView(R.layout.recordvideo);
		this.recordBtn = (ImageButton) super.findViewById(R.id.record);
		this.stopBtn = (ImageButton) super.findViewById(R.id.stop);
		this.browserBtn = (ImageButton) super.findViewById(R.id.browser);
		this.infoTxt = (TextView) super.findViewById(R.id.info);
		this.surfaceView = (SurfaceView) super.findViewById(R.id.surface);
		this.surfaceView.getHolder().setType(
				SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//设置缓冲类型
		this.surfaceView.getHolder().setFixedSize(480, 800);//设置分辨率
		if ((this.sdcardExists = Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED))) {//判断sd卡是否存在
			this.recordVideoSaveFileDir = new File(Environment
					.getExternalStorageDirectory().toString()
					+ File.separator
					+ VideoRecorderActivity.this.recDir + File.separator); // 保存录音目录
			if (!this.recordVideoSaveFileDir.exists()) {
				this.recordVideoSaveFileDir.mkdirs(); // 创建新文件夹
			}
		}
		this.recordBtn.setOnClickListener(new RecordOnClickListenerImpl());
		this.stopBtn.setOnClickListener(new StopOnClickListenerImpl());
		this.browserBtn
				.setOnClickListener(new BrowserOnClickListenerImpl());
		this.stopBtn.setEnabled(false); // 停止录象的按钮暂时不可用
	}

	/**
	 * 
	 * @author xdwang 
	 *
	 * @create 2012-11-15 下午09:25:32
	 * 
	 * @email:[email protected]
	 * 
	 * @description 视频录制
	 *
	 */
	private class RecordOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (VideoRecorderActivity.this.sdcardExists) { // sd卡存在
				VideoRecorderActivity.this.recordVideoSaveFileName = VideoRecorderActivity.this.recordVideoSaveFileDir
						.toString()
						+ File.separator
						+ "XdwangVideo_"
						+ System.currentTimeMillis() + ".3gp";// 文件的路径名称

				VideoRecorderActivity.this.recordVideoSaveFile = new File(
						VideoRecorderActivity.this.recordVideoSaveFileName);// 文件路径
				VideoRecorderActivity.this.mediaRecorder = new MediaRecorder();
				VideoRecorderActivity.this.mediaRecorder.reset(); // 重置
				VideoRecorderActivity.this.mediaRecorder
						.setVideoSource(MediaRecorder.VideoSource.CAMERA);// 设置视频源,从摄像头来
				VideoRecorderActivity.this.mediaRecorder
						.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置音频源,表示从麦克风进来
				VideoRecorderActivity.this.mediaRecorder
						.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4
				VideoRecorderActivity.this.mediaRecorder
						.setVideoEncoder(MediaRecorder.VideoEncoder.H264);// 设置录制的视频编码h263
																			// h264,注意这里如果写H263会出现错误
				VideoRecorderActivity.this.mediaRecorder
						.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);// 设置音频的编码
				VideoRecorderActivity.this.mediaRecorder
						.setOutputFile(VideoRecorderActivity.this.recordVideoSaveFileName);// 设置视频保存路径

				Camera camera = Camera.open(0); // 取得第一个摄像头,可能存在多个,0表示后面的,1表示前置摄像头
				Parameters parameters = camera.getParameters();
				List<Camera.Size> previewSizes = parameters
						.getSupportedPreviewSizes();
				Camera.Size pictureSize = previewSizes.get(0);

				VideoRecorderActivity.this.mediaRecorder.setVideoSize(
						pictureSize.width, pictureSize.height);// 视频分辨率

				// WindowManager wm = (WindowManager)
				// getSystemService(Context.WINDOW_SERVICE);
				// // 获取窗口服务
				// Display display = wm.getDefaultDisplay();// 获取屏幕信息
				// VideoRecorderActivity.this.mediaRecorder.setVideoSize(
				// display.getWidth(), display.getHeight());// 视频分辨率
				VideoRecorderActivity.this.mediaRecorder.setVideoFrameRate(10);// 设置每秒为10帧
				// 必须放在设置编码和格式的后面,否则报错
				VideoRecorderActivity.this.mediaRecorder
						.setPreviewDisplay(VideoRecorderActivity.this.surfaceView
								.getHolder().getSurface());// 定义视频显示
				try {
					VideoRecorderActivity.this.mediaRecorder.prepare();// 准备录像
				} catch (Exception e) {
					Log.i("VideoRecorderActivity", e.toString());
				}
				VideoRecorderActivity.this.mediaRecorder.start();// 开始录像
				VideoRecorderActivity.this.infoTxt.setText("正在录象中...");
				VideoRecorderActivity.this.stopBtn.setEnabled(true);
				VideoRecorderActivity.this.recordBtn.setEnabled(false);
				VideoRecorderActivity.this.isRecord = true;// 正在录像
			}
		}
	}

	/**
	 * 
	 * @author xdwang 
	 *
	 * @create 2012-11-15 下午09:30:23
	 * 
	 * @email:[email protected]
	 * 
	 * @description 停止录制视频
	 *
	 */
	private class StopOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (VideoRecorderActivity.this.isRecord) {
				VideoRecorderActivity.this.mediaRecorder.stop();

				VideoRecorderActivity.this.mediaRecorder.release();
				VideoRecorderActivity.this.stopBtn.setEnabled(false);
				VideoRecorderActivity.this.recordBtn.setEnabled(true);
				VideoRecorderActivity.this.infoTxt.setText("录象结束,文件路径为:"
						+ VideoRecorderActivity.this.recordVideoSaveFile);
			}
		}
	}

	/**
	 * 
	 * @author xdwang 
	 *
	 * @create 2012-11-15 下午09:36:36
	 * 
	 * @email:[email protected]
	 * 
	 * @description Activity跳转
	 *
	 */
	private class BrowserOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(VideoRecorderActivity.this,
					BroswerActivity.class);
			VideoRecorderActivity.this.startActivity(intent);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			super.finish();
		}
		return false;
	}

}
 

recordfiles.xml(BroswerActivity 程序进行ListView显示)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TableRow>
		<ImageView
			android:id="@+id/icon" 
			android:layout_width="fill_parent"
			android:layout_height="wrap_content" 
			android:src="@drawable/video_icon" />
		<TextView
			android:id="@+id/filename"
			android:textSize="25px"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
	</TableRow>
</TableLayout>

 

broswer.xml(BroswerActivity布局)

<?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">
	<LinearLayout 
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="horizontal" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:gravity="center">
		<TextView 
			android:id="@+id/info"
			android:textSize="25px"
			android:gravity="center"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" 
			android:text="视频文件列表" />
		<ImageButton
			android:id="@+id/back"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" 
			android:src="@drawable/back"/>
	</LinearLayout>
	<ListView
		android:id="@+id/videolist"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent" />
</LinearLayout>
 

BroswerActivity.java

package com.iflytek.demo;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/**
 * @author xdwang
 * 
 * @create 2012-11-15 下午10:15:22
 * 
 * @email:[email protected]
 * 
 * @description 视频文件浏览的Activity操作,提供ListView显示
 * 
 */

public class BroswerActivity extends Activity {
	private ImageButton backBtn = null;
	private ListView videolist = null;// 定义列表视图
	private SimpleAdapter recordSimpleAdapter = null;// 适配器
	private List<Map<String, Object>> recordFiles = null;// 保存所有的List数据
	private String recDir = "xdwangvideo";// 保存目录
	private File recordVideoSaveFileDir = null;// 文件保存目录
	private boolean sdcardExists = false;// sd卡是否存在

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题
		super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 高亮的风格显示
		super.setContentView(R.layout.broswer);
		if ((this.sdcardExists = Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED))) {
			this.recordVideoSaveFileDir = new File(Environment
					.getExternalStorageDirectory().toString()
					+ File.separator
					+ BroswerActivity.this.recDir + File.separator);
			if (!this.recordVideoSaveFileDir.exists()) {
				this.recordVideoSaveFileDir.mkdirs();
			}
		}
		this.backBtn = (ImageButton) super.findViewById(R.id.back);
		this.videolist = (ListView) super.findViewById(R.id.videolist);
		this.getRecordFiles();// 取得全部的文件列表
		this.videolist.setOnItemClickListener(new OnItemClickListenerImpl());
		this.backBtn.setOnClickListener(new BackOnClickListenerImpl());
	}

	/**
	 * @descrption 加载给定目录中的全部文件
	 * @author xdwang
	 * @create 2012-11-15下午10:26:55
	 */
	private void getRecordFiles() {// 取得全部录音文件
		this.recordFiles = new ArrayList<Map<String, Object>>();
		if (this.sdcardExists) {
			File files[] = this.recordVideoSaveFileDir.listFiles();
			for (int x = 0; x < files.length; x++) {
				Map<String, Object> fileInfo = new HashMap<String, Object>();
				fileInfo.put("filename", files[x].getName());// 增加显示内容
				this.recordFiles.add(fileInfo);// 保存数据
			}
			this.recordSimpleAdapter = new SimpleAdapter(this,
					this.recordFiles, R.layout.recordfiles,
					new String[] { "filename" }, new int[] { R.id.filename });// 实例化适配器
			this.videolist.setAdapter(this.recordSimpleAdapter);// 定义列表视图
		}
	}

	/**
	 * 
	 * @author xdwang
	 * 
	 * @create 2012-11-15 下午10:37:23
	 * 
	 * @email:[email protected]
	 * 
	 * @description 返回操作
	 * 
	 */
	private class BackOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent it = new Intent(BroswerActivity.this,
					VideoRecorderActivity.class);
			BroswerActivity.this.startActivity(it);
		}
	}

	/**
	 * 
	 * @author xdwang
	 * 
	 * @create 2012-11-15 下午10:43:41
	 * 
	 * @email:[email protected]
	 * 
	 * @description 单击视频选项进行播放
	 * 
	 */
	private class OnItemClickListenerImpl implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> adapter, View view,
				int position, long id) {// 选项单击
			if (BroswerActivity.this.recordSimpleAdapter.getItem(position) instanceof Map) {// 判断是否是Map实例
				Map<?, ?> map = (Map<?, ?>) BroswerActivity.this.recordSimpleAdapter
						.getItem(position);// 取得指定位置的内容
				Intent intent = new Intent(BroswerActivity.this,
						PlayVideoActivity.class);// 指定Intent
				intent.putExtra("filepath",
						BroswerActivity.this.recordVideoSaveFileDir.toString()
								+ File.separator
								+ map.get("filename").toString());
				BroswerActivity.this.startActivity(intent);// 启动Activity
			}
		}

	}
}

 

play.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">
	<LinearLayout 
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="horizontal" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
		<ImageButton
			android:id="@+id/play"
			android:layout_width="wrap_content"
			android:layout_height="fill_parent" 
			android:src="@drawable/play"/>
		<ImageButton
			android:id="@+id/stop"
			android:layout_width="wrap_content"
			android:layout_height="fill_parent" 
			android:src="@drawable/stop"/>
		<ImageButton
			android:id="@+id/back"
			android:layout_width="wrap_content"
			android:layout_height="fill_parent" 
			android:src="@drawable/back"/>
	</LinearLayout>
	<SurfaceView
		android:id="@+id/surfaceView"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent" />
</LinearLayout>
 

PlayVideoActivity.java

package com.iflytek.demo;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

/**
 * @author xdwang
 * 
 * @create 2012-11-15 下午10:50:57
 * 
 * @email:[email protected]
 * 
 * @description 播放视频的Activity,使用Mediaplayer完成播放
 * 
 */
public class PlayVideoActivity extends Activity {
	private ImageButton playBtn = null;
	private ImageButton stopBtn = null;
	private ImageButton backBtn = null;
	private MediaPlayer mediaPlayer = null;
	private SurfaceView sufaceView = null;
	private SurfaceHolder surfaceHolder = null;
	private String filepath = null;// 播放文件路径

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题
		super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 高亮的风格显示
		super.setContentView(R.layout.play);

		this.filepath = super.getIntent().getStringExtra("filepath");

		this.playBtn = (ImageButton) super.findViewById(R.id.play);
		this.stopBtn = (ImageButton) super.findViewById(R.id.stop);
		this.backBtn = (ImageButton) super.findViewById(R.id.back);
		this.sufaceView = (SurfaceView) super.findViewById(R.id.surfaceView);
		this.surfaceHolder = this.sufaceView.getHolder();
		this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 设置SurfaceView类型
		this.mediaPlayer = new MediaPlayer();
		this.mediaPlayer.reset();// 重置操作
		try {
			this.mediaPlayer.setDataSource(this.filepath);// 设置播放文件的路径
		} catch (Exception e) {
		}

		this.playBtn.setOnClickListener(new PlayOnClickListenerImpl());
		this.stopBtn.setOnClickListener(new StopOnClickListenerImpl());
		this.backBtn.setOnClickListener(new BackOnClickListenerImpl());
	}

	/**
	 * 
	 * @author xdwang
	 * 
	 * @create 2012-11-14 下午10:57:56
	 * 
	 * @email:[email protected]
	 * 
	 * @description 视频播放
	 * 
	 */
	private class PlayOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			PlayVideoActivity.this.mediaPlayer
					.setAudioStreamType(AudioManager.STREAM_MUSIC);// 设置音频类型
			PlayVideoActivity.this.mediaPlayer
					.setDisplay(PlayVideoActivity.this.surfaceHolder);// 设置显示区域
			try {
				PlayVideoActivity.this.mediaPlayer.prepare();// 预备状态
				PlayVideoActivity.this.mediaPlayer.start();// 播放视频
			} catch (Exception e) {
			}
		}
	}

	/**
	 * 
	 * @author xdwang
	 * 
	 * @create 2012-11-15 下午10:59:06
	 * 
	 * @email:[email protected]
	 * 
	 * @description 暂停视频
	 * 
	 */
	private class StopOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			PlayVideoActivity.this.mediaPlayer.stop();// 停止播放
		}
	}

	/**
	 * 
	 * @author xdwang
	 * 
	 * @create 2012-11-15下午11:03:16
	 * 
	 * @email:[email protected]
	 * 
	 * @description 返回
	 * 
	 */
	private class BackOnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent it = new Intent(PlayVideoActivity.this,
					BroswerActivity.class);
			PlayVideoActivity.this.startActivity(it);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.mediaPlayer.stop();
			this.mediaPlayer.release();
			super.finish();
		}
		return false;
	}
}

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iflytek.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".VideoRecorderActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".BroswerActivity"
            android:screenOrientation="landscape" />
        <activity
            android:name="PlayVideoActivity"
            android:screenOrientation="landscape" />
    </application>

</manifest>

你可能感兴趣的:(android,MediaRecorder)