android第二十步摄像
MediaRecorder攝像類
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);音頻取自麥克
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);視頻取自攝像機(jī)
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//格式3pg
recorder.setVideoSize(320, 240);視頻尺寸
recorder.setVideoFrameRate(5);視頻幀數(shù)
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);音頻編碼
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);視頻編碼
recorder.setOutputFile(file.getAbsolutePath());視頻保存位置
recorder.setPreviewDisplay(view.getHolder().getSurface());顯示在SurfaceView上
recorder.prepare();準(zhǔn)備錄制機(jī)開始捕獲編碼數(shù)據(jù)
recorder.start();開始
犯錯(cuò)記錄:
錯(cuò)誤
super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);requestWindowFeature(Window.FEATURE_NO_TITLE);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);正確
super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);setContentView(R.layout.activity_main);布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><SurfaceViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/surfaceView"/><RelativeLayout android:id="@+id/layout"android:visibility="gone"android:layout_width="match_parent"android:layout_height="match_parent"><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/recordbutton"android:id="@+id/recordbutton"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginRight="5dp"android:onClick="record"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/stopbutton"android:id="@+id/stopbutton"android:layout_marginRight="30dp"android:layout_toLeftOf="@id/recordbutton"android:layout_alignTop="@id/recordbutton"android:onClick="record"android:enabled="false"/></RelativeLayout></FrameLayout>程序
public class MainActivity extends Activity {private SurfaceView view;private RelativeLayout layout;private Button recordbutton;private Button stopbutton;private MediaRecorder recorder;@Overrideprotected 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);try{view = (SurfaceView)this.findViewById(R.id.surfaceView);view.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);view.getHolder().setFixedSize(176, 144);view.getHolder().setKeepScreenOn(true);layout = (RelativeLayout)this.findViewById(R.id.layout);recordbutton = (Button)this.findViewById(R.id.recordbutton);stopbutton = (Button)this.findViewById(R.id.stopbutton);}catch(Exception e){e.printStackTrace();}}public void record(View v){switch(v.getId()){case R.id.recordbutton:try{File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".3pg");recorder = new MediaRecorder();recorder.setAudioSource(MediaRecorder.AudioSource.MIC);recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);recorder.setVideoSize(320, 240);recorder.setVideoFrameRate(5);recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);recorder.setOutputFile(file.getAbsolutePath());recorder.setPreviewDisplay(view.getHolder().getSurface());recorder.prepare();recorder.start();recordbutton.setEnabled(false);stopbutton.setEnabled(true);}catch(Exception e){e.printStackTrace();}break;case R.id.stopbutton:if(recorder!=null){recorder.stop();recorder.release();recorder=null;}recordbutton.setEnabled(true);stopbutton.setEnabled(false);break;}}@Overridepublic boolean onTouchEvent(MotionEvent event) {if(event.getAction() == MotionEvent.ACTION_DOWN){layout.setVisibility(ViewGroup.VISIBLE);}return super.onTouchEvent(event);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu);return true;}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();}}權(quán)限
<uses-permission android:name="android.permission.RECORD_AUDIO"/><uses-permission android:name="android.permission.CAMERA"/><!-- 在SDCard中創(chuàng)建與刪除文件權(quán)限 --><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><!-- SDCard寫入數(shù)據(jù)權(quán)限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>?
轉(zhuǎn)載于:https://www.cnblogs.com/zyldream/p/3668156.html
總結(jié)
以上是生活随笔為你收集整理的android第二十步摄像的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UEditor 实现新增“转换简体”和“
- 下一篇: flashbackup闪回数据库