首页 > 其他 > 详细

自定义录像机 没任何新意

时间:2016-01-10 17:13:16      阅读:247      评论:0      收藏:0      [点我收藏+]

自定义录像机的例子,没什么新意

package com.example.ggpla.myapplication;

import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.ggpla.utils.FileOptionUtils;

import java.io.IOException;

public class MyVideoCamera extends AppCompatActivity implements SurfaceHolder.Callback{

    private Camera myCamera;

    private SurfaceView mySView;
    private SurfaceHolder myHolder;

    private ToggleButton toggleButton;

    private boolean mIsRecording = false;
    private MediaRecorder mediaRecorder;

    private String videoUrl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main22);

        mySView = (SurfaceView)findViewById(R.id.surfaceView2);
        myHolder = mySView.getHolder();
        myHolder.addCallback(this);
        myHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        toggleButton = (ToggleButton)findViewById(R.id.toggleButton2);
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {//true时,从关闭状态到开启状态
                    startmediaRecorder();
                    Toast.makeText(MyVideoCamera.this,"开始录像",Toast.LENGTH_LONG).show();
                } else {//false时,从开启状态到关闭状态
                    stopmediaRecorder();
                    Toast.makeText(MyVideoCamera.this,"录像完成",Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        openCamera();
        if (myHolder != null){
            setStartPreview(myHolder,myCamera);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseCamera();
    }

    /**
     *获取Camera硬件对象
     */
    private void openCamera(){
        if(myCamera == null){
            myCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
        }
    }

    /**
     * 释放相机资源
     */
    private void releaseCamera(){
        if(myCamera != null){
            myCamera.setPreviewCallback(null);
            myCamera.stopPreview();
            myCamera.release();
            myCamera = null;
        }
    }


    //开始
    private void setStartPreview(SurfaceHolder surfaceHolder,Camera camera){
        try {
            camera.setPreviewDisplay(surfaceHolder);
            //将系统预览角度设置为横屏
            camera.setDisplayOrientation(90);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        setStartPreview(myHolder,myCamera);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        myCamera.stopPreview();
        setStartPreview(myHolder,myCamera);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        releaseCamera();
    }


    /**
     * 结束录制方法
     */
    private void stopmediaRecorder() {
        if(mediaRecorder!=null){
            if(mIsRecording){
                mediaRecorder.stop();
                //mCamera.lock();
                mediaRecorder.reset();
                mediaRecorder.release();
                mediaRecorder=null;
                mIsRecording = false;
                try {
                    myCamera.reconnect();
                } catch (IOException e) {
                    Toast.makeText(this, "reconect fail",Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 开始录像方法
     */
    private void startmediaRecorder() {
        //自定义的静态方法,http://8779947.blog.51cto.com/8769947/1733467中有介绍FileOptionUtils工具类
        videoUrl = FileOptionUtils.getSavePath(FileOptionUtils.NAMED_BY_SELF, "test", "MP4", "VIDEO");
        myCamera.unlock();
        mIsRecording = true;
        mediaRecorder = new MediaRecorder();
        mediaRecorder.reset();
        mediaRecorder.setCamera(myCamera);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        //设置录像机拍摄参数,与下面参数设置只能二选一
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
      mediaRecorder.setVideoFrameRate(5);
      mediaRecorder.setVideoSize(640, 480);
//设置录像机拍摄参数,与下面参数设置只能二选一,CamcorderProfile.QUALITY_HIGH为拍摄质量。
//        CamcorderProfile mCamcorderProfile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_BACK, CamcorderProfile.QUALITY_HIGH);
//        mediaRecorder.setProfile(mCamcorderProfile);
        mediaRecorder.setOutputFile(videoUrl);
        mediaRecorder.setPreviewDisplay(myHolder.getSurface());

        try {
            mediaRecorder.prepare();
        } catch (Exception e) {

            mIsRecording = false;
            Toast.makeText(MyVideoCamera.this, "fail", Toast.LENGTH_LONG).show();
            e.printStackTrace();
            myCamera.lock();
        }
        mediaRecorder.start();
    }

}


本文出自 “不会笑” 博客,请务必保留此出处http://8779947.blog.51cto.com/8769947/1733471

自定义录像机 没任何新意

原文:http://8779947.blog.51cto.com/8769947/1733471

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!