示例#1
0
        private bool prepareVideoRecorder()
        {
            // BEGIN_INCLUDE (configure_preview)
            mCamera = CameraHelper.getDefaultCameraInstance();

            // We need to make sure that our preview and recording video size are supported by the
            // camera. Query camera to find all the sizes and choose the optimal size given the
            // dimensions of our preview surface.
            Camera.Parameters   parameters             = mCamera.GetParameters();
            IList <Camera.Size> mSupportedPreviewSizes = parameters.SupportedPreviewSizes;
            IList <Camera.Size> mSupportedVideoSizes   = parameters.SupportedVideoSizes;

            Camera.Size optimalSize = CameraHelper.getOptimalVideoSize(mSupportedVideoSizes,
                                                                       mSupportedPreviewSizes, mPreview.Width, mPreview.Height);

            // Use the same size for recording profile.
            CamcorderProfile profile = CamcorderProfile.Get(CamcorderQuality.High);

            profile.VideoFrameWidth  = optimalSize.Width;
            profile.VideoFrameHeight = optimalSize.Height;

            // likewise for the camera object itself.
            parameters.SetPreviewSize(profile.VideoFrameWidth, profile.VideoFrameHeight);
            mCamera.SetParameters(parameters);
            try
            {
                // Requires API level 11+, For backward compatibility use {@link setPreviewDisplay}
                // with {@link SurfaceView}
                mCamera.SetPreviewTexture(mPreview.SurfaceTexture);
            }
            catch (IOException e)
            {
                Log.Error(TAG, "Surface texture is unavailable or unsuitable" + e.Message);
                return(false);
            }
            // END_INCLUDE (configure_preview)


            // BEGIN_INCLUDE (configure_media_recorder)
            mMediaRecorder = new Android.Media.MediaRecorder();

            // Step 1: Unlock and set camera to MediaRecorder
            mCamera.Unlock();
            mMediaRecorder.SetCamera(mCamera);

            // Step 2: Set sources
            mMediaRecorder.SetAudioSource(AudioSource.Default);
            mMediaRecorder.SetVideoSource(VideoSource.Camera);

            // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
            mMediaRecorder.SetProfile(profile);

            // Step 4: Set output file
            mOutputFile = CameraHelper.getOutputMediaFile(CameraHelper.MEDIA_TYPE_VIDEO);
            if (mOutputFile == null)
            {
                return(false);
            }
            mMediaRecorder.SetOutputFile(mOutputFile.Path);
            // END_INCLUDE (configure_media_recorder)

            // Step 5: Prepare configured MediaRecorder
            try
            {
                mMediaRecorder.Prepare();
            }
            catch (IllegalStateException e)
            {
                Log.Debug(TAG, "IllegalStateException preparing MediaRecorder: " + e.Message);
                releaseMediaRecorder();
                return(false);
            }
            catch (IOException e)
            {
                Log.Debug(TAG, "IOException preparing MediaRecorder: " + e.Message);
                releaseMediaRecorder();
                return(false);
            }
            return(true);
        }
示例#2
0
		//PREPARAMOS NUESTRO MEDIA RECORD Y CAMARA
		private bool PrepararVideoRecorder()
		{
			//OBTENEMOS LA INSTANCIA DE NUESTRA CAMARA YA PREPARADA
			_camera = ObtenerInstanciaCamara();
			//INICIALIZAMOS NUESTRA VARIABLE MEDIARECORDER
			_mediaRecorder = new MediaRecorder();

			//LE INDICAMOS A NUESTRA CAMARA QUE CAMBIE DE ROTACION ESTO ES DEVIDO QUE POR DEFUALT NOS MUESTRA EN POSICION HORIZONTAL,
			//Y COMO DEFINIMOS QUE LA POSICION DE NUESTRA APP SEA Portrait, REALIZAMOS EL CAMBIO.
			_camera.SetDisplayOrientation (90);
			//ABRIMOS NUESTRA CAMARA PARA SER USADA
			_camera.Unlock();


			//DE IGUAL FORMA QUE NUESTRA CAMARA CAMBIAMOS LA POSICION DE NUESTRA MEDIARECORDER
			_mediaRecorder.SetOrientationHint (90);
			//ASIGNAMOS LA CAMARA A NUESTRA MEDIARECORDER
			_mediaRecorder.SetCamera(_camera);

			//ASIGNAMOS LOS FORMATOS DE VIDEO Y AUDIO
			_mediaRecorder.SetAudioSource(AudioSource.Camcorder);
			_mediaRecorder.SetVideoSource(VideoSource.Camera);

			//RECUPERAMOS EL PERFIL QUE TIENE NUESTRA CAMARA PARA PODER ASIGNARSELA A NUESTRA MEDIARECORDER
			var camcorderProfile = ((int)Build.VERSION.SdkInt) >= 9 ? CamcorderProfile.Get(0, CamcorderQuality.High) : CamcorderProfile.Get(CamcorderQuality.High);

			//ASIGNAMOS EL PERFIL A NUESTRO MEDIARECORDER
			_mediaRecorder.SetProfile(camcorderProfile);

			//LE ASIGNAMOS EL PATH DONDE SE ENCUESTRA NUESTRO ARCHIVO DE VIDEO PARA PODER CREARLO
			_mediaRecorder.SetOutputFile(PathArchivoVideo ());


			//ASIGNAMOS EL SURFACE A NUESTRO MEDIARECORDER QUE UTILIZARA PARA VISUALIZAR LO QUE ESTAMOS GRABANDO
			_mediaRecorder.SetPreviewDisplay(_videoView.Holder.Surface);
			try
			{
				//CONFIRMAMOS LOS CAMBIOS HECHOS EN NUESTRO MEDIA RECORDER PARA PODER INICIAR A GRABAR
				_mediaRecorder.Prepare();
				return true;
			}
			catch
			{
				//SI OCURRE ALGUN PROBLEMA LIBRAMOS LOS RECURSOS ASIGNADOS A NUESTRO MEDIARECORDER
				LiberarMediaRecorder();
				return false;
			}
		}