基本信息
源码名称:android 调用摄像头录制视频并播放 实例源码下载
源码大小:0.73M
文件格式:.zip
开发语言:Java
更新时间:2015-01-13
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 3 元 
   源码介绍


package cn.yws.video;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.VideoView;

//http://www.cnblogs.com/stay/archive/2011/06/24/2089129.html

public class VideoActivity extends Activity {

	private File myRecAudioFile;
	private SurfaceView mSurfaceView;
	private SurfaceHolder mSurfaceHolder;
	private Button buttonStart;
	private Button buttonStop;
	private Button buttonPlay, buttonPlay2;
	private File dir;
	private MediaRecorder recorder;
	private RadioButton backRadioButton, frontRadioButton;
	private Camera camera;
	private VideoView videoView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mSurfaceView = (SurfaceView) findViewById(R.id.videoView);
		mSurfaceHolder = mSurfaceView.getHolder();
		mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		
		mSurfaceHolder.setKeepScreenOn(true);
		
		buttonStart = (Button) findViewById(R.id.start);
		buttonStop = (Button) findViewById(R.id.stop);
		buttonPlay = (Button) this.findViewById(R.id.play);
		buttonPlay2 = (Button) this.findViewById(R.id.play2);

		backRadioButton = (RadioButton) this.findViewById(R.id.back);
		frontRadioButton = (RadioButton) this.findViewById(R.id.front);
		videoView = (VideoView) this.findViewById(R.id.video_view);

		File defaultDir = Environment.getExternalStorageDirectory();
		String path = defaultDir.getAbsolutePath()   File.separator   "V"
				  File.separator;
		// 创建文件夹存放视频
		dir = new File(path);
		if (!dir.exists()) {
			dir.mkdir();
		}
		

		buttonStart.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				recorder();
			}
		});

		buttonStop.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(recorder!=null)
				{
					recorder.stop();
					recorder.reset();
				}
				if(camera!=null)
				{
					camera.stopPreview();
				}
				// camera.release();
				// camera=null;
				// recorder.release();
				// recorder = null;
				
			}
		});
		buttonPlay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (myRecAudioFile != null) {
					String path = myRecAudioFile.getAbsolutePath();
					Uri uri = Uri.parse(path);
					Log.i("path", uri.getPath());
					// videoView.setMediaController(new MediaController(this));
					videoView.setVideoURI(uri);
					
					videoView.start();
					videoView.requestFocus();

				}

			}
		});
		buttonPlay2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(VideoActivity.this,
						VideoActivity2.class);
				intent.putExtra("filePath", myRecAudioFile.getAbsolutePath());
				startActivity(intent);
			}
		});
	}

	private void destory(boolean force) {

		if (recorder != null) {
			if (!force) {
				recorder.stop();
			}
			recorder.reset();
			recorder.release();
			recorder = null;
		}

		if (camera != null) {
			camera.lock();
			camera.release();
			camera = null;
		}
	}

	@Override
	protected void onDestroy() {

		destory(true);
		super.onDestroy();
	}

	@SuppressLint("NewApi")
	public void recorder() {
		try {
			
			destory(true);
			recorder = new MediaRecorder();
			recorder.setOrientationHint(90);//旋转度数
			int index = CameraTool.FindBackCamera();
			if (frontRadioButton.isChecked()) {
				index = CameraTool.FindFrontCamera();
				recorder.setOrientationHint(270);//旋转度数
			}
			
			
			/**if(camera!=null)
			{
				camera.release();
				camera=null;
			}*/				
			camera = Camera.open(index);
			camera.setDisplayOrientation(90);//旋转度数
			// camera.setPreviewDisplay(mSurfaceHolder);
			// camera.startPreview();
			//http://www.cnblogs.com/stay/archive/2011/06/24/2089129.html
			camera.unlock();// 注意,要在MediaRecorder设置参数之前就调用unlock来获得camera的控制权。camera是单例的嘛。如果不调用,程序就挂
			
			
			recorder.setCamera(camera);

			myRecAudioFile = new File(dir, "video.mp4");
			// myRecAudioFile = File.createTempFile("video", ".3gp", dir);//
			// 创建临时文件
			recorder.setPreviewDisplay(mSurfaceHolder.getSurface());// 预览
			recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);// 视频源
			recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 录音源为麦克风
			recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);// 输出格式为3gp
			recorder.setVideoSize(800, 480);// 视频尺寸
			recorder.setVideoFrameRate(5);// 视频帧频率
			recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);// 视频编码
			recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);// 音频编码
			recorder.setMaxDuration(10000);// 最大期限
			
			recorder.setOutputFile(myRecAudioFile.getAbsolutePath());// 保存路径
			recorder.prepare();
			recorder.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}