自定義錄像機的例子,沒什么新意
十年的玉環(huán)網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都營銷網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整玉環(huán)建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“玉環(huán)網(wǎng)站設(shè)計”,“玉環(huán)網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
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時,從關(guān)閉狀態(tài)到開啟狀態(tài) startmediaRecorder(); Toast.makeText(MyVideoCamera.this,"開始錄像",Toast.LENGTH_LONG).show(); } else {//false時,從開啟狀態(tài)到關(guān)閉狀態(tài) 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); //將系統(tǒng)預(yù)覽角度設(shè)置為橫屏 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(); } /** * 結(jié)束錄制方法 */ 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() { //自定義的靜態(tài)方法,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); //設(shè)置錄像機拍攝參數(shù),與下面參數(shù)設(shè)置只能二選一 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mediaRecorder.setVideoFrameRate(5); mediaRecorder.setVideoSize(640, 480); //設(shè)置錄像機拍攝參數(shù),與下面參數(shù)設(shè)置只能二選一,CamcorderProfile.QUALITY_HIGH為拍攝質(zhì)量。 // 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(); } }