网站建设资讯

NEWS

网站建设资讯

自定义录像机没任何新意

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

十年的玉环网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整玉环建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“玉环网站设计”,“玉环网站推广”以来,每个客户项目都认真落实执行。

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://cdweb.net/article/gdipsi.html