实例教程十四:视频刻录(调用前置摄像头)

安卓txt电子书阅读器源码分享
http://www.eoeandroid.com/thread-225771-1-1.html

仿UCWEB界面源码!
http://www.eoeandroid.com/thread-225509-1-1.html

酒店预订系统
http://www.eoeandroid.com/thread-225529-1-1.html

------------帖子正文---------------------

<?xml version="1.0" encoding="utf-8"?>   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="vertical" >           <!-- 父界面 -->           <SurfaceView           android:id="@+id/sfvShow"          android:layout_width="fill_parent"          android:layout_height="fill_parent" />           <!-- 子界面 -->           <RelativeLayout           android:id="@+id/btnLayout"          android:layout_width="fill_parent"          android:layout_height="fill_parent"          android:visibility="gone" >               <Button               android:id="@+id/btnRecord"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_alignParentBottom="true"              android:layout_alignParentRight="true"              android:layout_marginRight="5dp"              android:onClick="record"              android:text="@string/btnRecord" />               <Button               android:enabled="false"              android:id="@+id/btnStop"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_alignTop="@id/btnRecord"              android:layout_marginRight="20dp"              android:layout_toLeftOf="@id/btnRecord"              android:onClick="record"              android:text="@string/btnStop" />       </RelativeLayout>       </FrameLayout>
<?xml version="1.0" encoding="utf-8"?>   <manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="cn.itcast.recorder"      android:versionCode="1"      android:versionName="1.0" >           <uses-sdk android:minSdkVersion="8" />           <application           android:icon="@drawable/ic_launcher"          android:label="@string/app_name" >           <activity               android:name=".MainActivity"              android:label="@string/app_name"              android:screenOrientation="landscape" >               <intent-filter>                   <action android:name="android.intent.action.MAIN" />                       <category android:name="android.intent.category.LAUNCHER" />               </intent-filter>           </activity>       </application>               <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" />       </manifest>
package cn.itcast.recorder;       import java.io.File;       import android.app.Activity;   import android.hardware.Camera;   import android.media.MediaRecorder;   import android.os.Bundle;   import android.os.Environment;   import android.view.MotionEvent;   import android.view.SurfaceHolder;   import android.view.SurfaceView;   import android.view.View;   import android.view.Window;   import android.view.WindowManager;   import android.widget.Button;   import android.widget.RelativeLayout;   import android.hardware.Camera.Parameters;       public class MainActivity extends Activity {           private SurfaceView sfvShow;           private RelativeLayout btnLayout;           private Button btnRecord;           private Button btnStop;           private MediaRecorder mediaRecorder;               private Camera camera = null;                   private Camera.CameraInfo cameraInfo;           private int cameraCount = 0;               @Override          public 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.main);                       initView();           }               private void initView(){                   btnRecord = (Button)this.findViewById(R.id.btnRecord);                   btnStop = (Button)this.findViewById(R.id.btnStop);                       btnLayout = (RelativeLayout)this.findViewById(R.id.btnLayout);                   sfvShow = (SurfaceView)this.findViewById(R.id.sfvShow);                       sfvShow.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);                   sfvShow.getHolder().setFixedSize(176, 144);                   sfvShow.getHolder().setKeepScreenOn(true); //使摄像头一直保持高亮           }               //触摸屏幕事件           @Override          public boolean onTouchEvent(MotionEvent event) {                   if(event.getAction() == MotionEvent.ACTION_DOWN){                           btnLayout.setVisibility(View.VISIBLE);                   }                   return super.onTouchEvent(event);           }               public void record(View v){                   switch(v.getId()){                   case R.id.btnRecord:                           try{                                   initCamera(); //调用前置摄像头--注意,要在MediaRecorder设置参数之前就调用unlock来获得camera的控制权。camera是单例的嘛。如果不调用,程序就挂                                   File videofile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".3gp");                                   mediaRecorder = new MediaRecorder();                                   mediaRecorder.setCamera(camera); //如果需要前置摄像头,则加上,反之,这句话不需要存在                                   //设置声音从哪个设备获取                                   mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);                                   //设置画面从哪个设备获取                                   mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);                                   //设置输入的格式                                   mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);                                   //设置视频大小                                   mediaRecorder.setVideoSize(320, 240);                                   //设置每秒获取几帧,5帧比较清晰                                   mediaRecorder.setVideoFrameRate(5);                                   //设置音频编码格式                                   mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);                                   //设置视频编码格式                                   mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);                                   //设置文件存放位置                                   mediaRecorder.setOutputFile(videofile.getAbsolutePath());                                   //显示画面                                   mediaRecorder.setPreviewDisplay(sfvShow.getHolder().getSurface()); //预览输入                                   //缓冲                                   mediaRecorder.prepare();                                   //开始刻录                                   mediaRecorder.start();                           }catch(Exception e){                                   e.printStackTrace();                           }                           btnRecord.setEnabled(false);                           btnStop.setEnabled(true);                               break;                   case R.id.btnStop:                           if(mediaRecorder != null){                                   //停止记录                                   mediaRecorder.stop();                                   //释放该对象                                   mediaRecorder.release();                           }                               btnRecord.setEnabled(true);                           btnStop.setEnabled(false);                           break;                   default:                           break;                   }           }               private void initCamera(){                   Camera.CameraInfo cameraInfo = new Camera.CameraInfo();                     cameraCount = Camera.getNumberOfCameras(); // get cameras number                         for ( int camIdx = 0; camIdx < cameraCount;camIdx++ ) {                             Camera.getCameraInfo( camIdx, cameraInfo ); // get camerainfo                             if ( cameraInfo.facing ==Camera.CameraInfo.CAMERA_FACING_FRONT ) {                                    // 代表摄像头的方位,目前有定义值两个分别为CAMERA_FACING_FRONT前置和CAMERA_FACING_BACK后置                                     try {                                                         camera = Camera.open(camIdx);                                     } catch (RuntimeException e) {                                             e.printStackTrace();                                     }                             }                   }                   camera.unlock();           }   }

2012-10-10 15:27 上传
下载附件 (9.76 KB)
 

调用的前置摄像头,照的是天花板

你可能感兴趣的:(移动开发,adnroid开发,视频刻录)