package com.example.view_player;
import java.io.File;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private SurfaceView surfaceView; //视频显示区域的声明
private RelativeLayout layout; //相对布局的声明
private Callback callback; //
private Camera camera;
private MediaRecorder mediaRecorder; //媒体录制
private Button start; //按钮
private Button stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
start = (Button) this.findViewById(R.id.start);
this.stop = (Button) this.findViewById(R.id.stop);
layout = (RelativeLayout) this.findViewById(R.id.layout);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
callback = new MyCallback();
surfaceView.getHolder().addCallback(callback);
}
private class MyCallback implements Callback //回调类
{
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
try
{
camera = Camera.open(); //打开摄像
camera.setPreviewDisplay(surfaceView.getHolder());
camera.startPreview(); //开始预览
}catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
if(camera != null)
{
camera.stopPreview(); //停止预览
camera.release(); //释放资源
camera = null;
}
}
}
public void startVoide(View v)
{
switch(v.getId())
{
case R.id.start:
start.setEnabled(false);
stop.setEnabled(true);
try
{
File file = new File(Environment.getExternalStorageDirectory()
+ "/" + System.currentTimeMillis() + ".3gp");
camera.unlock();
mediaRecorder = new MediaRecorder(); //媒体录制对象
mediaRecorder.setCamera(camera); //设置摄像
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //设置输出的文件的格式
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //设置编码
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setOutputFile(file.getAbsolutePath()); //设置输出文件的路径
mediaRecorder.setVideoSize(320, 240); //设置video的大小
mediaRecorder.setVideoFrameRate(5);
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
mediaRecorder.prepare(); //缓冲
mediaRecorder.start(); //开始录制
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
break;
case R.id.stop:
start.setEnabled(true);
stop.setEnabled(false);
if(mediaRecorder != null)
{
mediaRecorder.stop(); //停止
mediaRecorder.reset(); //重置
mediaRecorder.release(); //释放资源
camera.lock();
mediaRecorder = null;
}
break;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
layout.setVisibility(ViewGroup.VISIBLE); //当触屏的时候按钮可见
return super.onTouchEvent(event);
}
}