代码改变世界

Android video recording demo

2010-11-18 13:24  RayLee  阅读(1186)  评论(1编辑  收藏  举报

UI

提供三个功能按钮:‘Start video recording’,‘Stop video recording’和‘Play video’。

Android平台针对视频设计了一个更抽象的控件VideoView。下面是UI的XML 文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
  	<VideoView android:id="@+id/videoView"
			android:layout_width="300px"
			android:layout_height="300px"
			android:layout_marginRight="10px" />
			
	<LinearLayout android:orientation="vertical"
		android:layout_width="wrap_content"
		android:layout_height="fill_parent">
		
		<Button android:id="@+id/startVideoRecording"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:text="Start video recording" />
				
		<Button android:id="@+id/stopVideoRecording"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:text="Stop video recording" />
				
		<Button android:id="@+id/playVideo"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:text="Play video" />	
	</LinearLayout>
</LinearLayout>

功能实现

public class VideoPlay extends Activity {
	
	Button btn_startVideoRecording;
	Button btn_stopVideoRecording;
	Button btn_playVideo;
	VideoView mVideoView;
	MediaRecorder mRecorder;
	SurfaceHolder mHolder;
	
	final String OUTPUT_FILE = "/sdcard/videoRecording.mp4";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.video_play);

		mVideoView = (VideoView)findViewById(R.id.videoView);
		btn_startVideoRecording = (Button)findViewById(R.id.startVideoRecording);
		btn_stopVideoRecording = (Button)findViewById(R.id.stopVideoRecording);
		btn_playVideo = (Button)findViewById(R.id.playVideo);
		
		mHolder = mVideoView.getHolder();
		mHolder.addCallback(this);
        
		btn_playVideo.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mVideoView.setVideoPath(OUTPUT_FILE);
				mVideoView.start();
			}
		});
		
		btn_startVideoRecording.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startVideoRecording();
			}
		});
		
		btn_stopVideoRecording.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				stopVideoRecording();
			}
		});
	}
	
	private void startVideoRecording() {
		mRecorder = new MediaRecorder();
		
		mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
		
		mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
		
		mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		mRecorder.setOutputFile(OUTPUT_FILE);
		mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
		mRecorder.setVideoFrameRate(20);
		mRecorder.setVideoSize(352, 288);
		
		try {
			mRecorder.setPreviewDisplay(mHolder.getSurface());
			mRecorder.prepare();
		} catch (Exception e) {
			mRecorder.reset();
	        mRecorder.release();
	        mRecorder = null;
	        return;
		}
		
		mRecorder.start();
		Log.d("VideoPlay", "Start video recording");
	}
	
	private void stopVideoRecording() {
		if (mRecorder == null)
			return;
		
		mRecorder.reset();
		mRecorder.release();
		mRecorder = null;
		Log.d("VideoPlay", "Stop video recording");
	}
}

附一张截图:

2