android第二十步摄像
MediaRecorder摄像类
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);音频取自麦克
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);视频取自摄像机
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//格式3pg
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());显示在SurfaceView上
recorder.prepare();准备录制机开始捕获编码数据
recorder.start();开始
犯错记录:
错误
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" > <SurfaceView android: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; @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); 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; } } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ layout.setVisibility(ViewGroup.VISIBLE); } return super.onTouchEvent(event); } @Override public 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; } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }
权限
<uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.CAMERA"/> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>