public void StartRecord(string filename)
        {
            if (recorder != null)
            {
                recorder.Reset();
            }
            else
            {
                recorder = new MediaRecorder();
            }
            mCamera.Lock();
            mCamera.Unlock();

            recorder.SetCamera(mCamera);
            recorder.SetVideoSource(VideoSource.Camera);
            recorder.SetAudioSource(AudioSource.Camcorder);

            try
            {
                //try for full hd
                recorder.SetProfile(CamcorderProfile.Get(CURRENTCAMERA, CamcorderQuality.Q1080p));
            }
            catch
            {
                try
                {
                    recorder.SetProfile(CamcorderProfile.Get(CURRENTCAMERA, CamcorderQuality.Q720p));
                }
                catch
                {
                    try
                    {
                        //if not hd, try for highest phone can give
                        recorder.SetProfile(CamcorderProfile.Get(CURRENTCAMERA, CamcorderQuality.High));
                    }
                    catch
                    {
                        //if not, then set to low
                        recorder.SetProfile(CamcorderProfile.Get(CURRENTCAMERA, CamcorderQuality.Q480p));
                    }
                }
            }

            //recorder.SetAudioEncoder(AudioEncoder.Aac);
            //recorder.SetVideoEncoder(VideoEncoder.H264);
            recorder.SetPreviewDisplay(mPreview.Holder.Surface);
            recorder.SetOutputFile(filename);
            recorder.SetMaxDuration(1000 * 60 * 5);

            try
            {
                recorder.Prepare();
                recorder.Start();
            }
            catch (Exception e)
            {
                OnError?.Invoke(e.ToString());
            }
        }
Esempio n. 2
0
        public void StartRecording()
        {
            var mManager = MainActivity.CurrentActivity.GetSystemService(Context.MediaProjectionService) as MediaProjectionManager;

            mediaProjection = mManager.GetMediaProjection((int)MainActivity.ReturnResultFromPermission, MainActivity.ReturnDataFromPermission);
            DisplayManager dManager       = GetSystemService(Context.DisplayService) as DisplayManager;
            var            displayMetrics = new DisplayMetrics();

            dManager.GetDisplay(0).GetMetrics(displayMetrics);

            mediaRecorder = new MediaRecorder();
            mediaRecorder.SetAudioSource(AudioSource.Mic);
            mediaRecorder.SetVideoSource(VideoSource.Surface);

            var profile = CamcorderProfile.Get(CamcorderQuality.High);

            profile.FileFormat       = OutputFormat.Mpeg4;
            profile.VideoFrameHeight = displayMetrics.HeightPixels;
            profile.VideoFrameWidth  = displayMetrics.WidthPixels;

            mediaRecorder.SetProfile(profile);
            mediaRecorder.SetOutputFile($"{Android.OS.Environment.ExternalStorageDirectory}/demovideo.mp4");
            mediaRecorder.Prepare();

            recordingDisplay = mediaProjection.CreateVirtualDisplay("Rec display", displayMetrics.WidthPixels, displayMetrics.HeightPixels,
                                                                    (int)displayMetrics.Density, Android.Views.DisplayFlags.Round,
                                                                    mediaRecorder.Surface, null, null);

            mediaRecorder.Start();
        }
 private async Task RecordVideo()
 {
     if (!(Element as VideoCameraPage).IsRecording)
     {
         string filepath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
         string filename = System.IO.Path.Combine(filepath, "video.mp4");
         if (File.Exists(filename))
         {
             File.Delete(filename);
         }
         recorder = new MediaRecorder();
         camera.Unlock();
         recorder.SetCamera(camera);
         recorder.SetVideoSource(VideoSource.Camera);
         recorder.SetAudioSource(AudioSource.Mic);
         recorder.SetProfile(CamcorderProfile.Get(0, CamcorderQuality.High));
         //recorder.SetVideoEncoder(VideoEncoder.Default);
         //recorder.SetAudioEncoder(AudioEncoder.Default);
         //recorder.SetOutputFormat(OutputFormat.Mpeg4);
         recorder.SetOutputFile(filename);
         recorder.Prepare();
         recorder.Start();
         (Element as VideoCameraPage).IsRecording = true;
     }
 }
Esempio n. 4
0
        public void StartRecording(object sender, EventArgs e)
        {
            ////Make sure we're in a good state before we start recording
            //if (!XamRecorder.IsPreviewing)
            //{
            //	throw new Exception("You can't start recording until you are previewing.");
            //}

            if (XamRecorder.IsRecording)
            {
                throw new Exception("You can't start recording because you are already recording.");
            }

            //Set path for the video file
            string filepath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            string filename = Path.Combine(filepath, "video.mp4");

            //string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/video.mp4";
            XamRecorder.VideoFileName = filename;

            if (IsCameraAvailable)
            {
                //Delete the file if it already exists
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }


                //Start recording
                recorder = new MediaRecorder();
                camera.Unlock();
                recorder.SetCamera(camera);
                recorder.SetVideoSource(VideoSource.Camera);
                recorder.SetAudioSource(AudioSource.Mic);
                recorder.SetProfile(CamcorderProfile.Get(cameraId, CamcorderQuality.High));
                //recorder.SetVideoEncoder(VideoEncoder.Default); //Also tried default
                //recorder.SetAudioEncoder(AudioEncoder.Default); //Also tried default
                //recorder.SetOutputFormat(OutputFormat.Mpeg4);
                recorder.SetOutputFile(filename);

                recorder.SetPreviewDisplay(holder.Surface);

                recorder.Prepare();
                recorder.Start();
            }
            else
            {
                //Copy sample video to destiantion path just to have something there.
                FileInfo sample = new FileInfo("sample.mp4");
                sample.CopyTo(filename);
            }

            XamRecorder.IsPreviewing = true;
            XamRecorder.IsRecording  = true;
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
0
        private void SetUpMediaRecorder()
        {
            if (null == _context)
            {
                return;
            }
            mediaRecorder = new MediaRecorder();
            //changed this camcorder
            mediaRecorder.SetAudioSource(AudioSource.Camcorder);
            mediaRecorder.SetVideoSource(VideoSource.Surface);
            // mediaRecorder.SetOutputFormat(OutputFormat.Mpeg4);

            //profile should be set after audio and video source and before
            //setting the output file
            //set this for test A
            mediaRecorder.SetProfile(CamcorderProfile.Get(CamcorderQuality.High));

            string localFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);

            videoFilePath = System.IO.Path.Combine(localFolder, $"video_{DateTime.Now.ToString("yyMMdd_hhmmss")}.mp4");


            // var localPath = Android.OS.Environment.ExternalStorageDirectory + "/video1.mp4";
            mediaRecorder.SetOutputFile(videoFilePath);

            //mediaRecorder.SetVideoEncodingBitRate(10000000);
            //mediaRecorder.SetVideoFrameRate(30);

            // Call this after setOutFormat() but before prepare().
            mediaRecorder.SetVideoSize(videoSize.Width, videoSize.Height);

            //mediaRecorder.SetVideoEncoder(VideoEncoder.H264);
            //mediaRecorder.SetAudioEncoder(AudioEncoder.Aac);

            var windowManager = _context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>();
            int rotation      = (int)windowManager.DefaultDisplay.Rotation;
            int orientation   = ORIENTATIONS.Get(rotation);

            mediaRecorder.SetOrientationHint(orientation);
            mediaRecorder.Prepare();
        }
Esempio n. 7
0
        private bool PrepareMediaRecorder()
        {
            camera   = Camera.Open();
            recorder = new MediaRecorder();

            camera.Unlock();
            recorder.SetCamera(camera);

            recorder.SetAudioSource(AudioSource.Camcorder);
            recorder.SetVideoSource(VideoSource.Camera);

            recorder.SetProfile(CamcorderProfile.Get(CamcorderQuality.High));

            recorder.SetOutputFile(GetOutputMediaFile(MediaType.Video).ToString());

            recorder.SetPreviewDisplay(preview.Holder.Surface);

            try
            {
                recorder.Prepare();
            }
            catch (IllegalStateException e)
            {
                Log.Debug("PrepareMediaRecorder", "Illegal state: " + e.Message);
                ReleaseMediaRecorder();
                return(false);
            }
            catch (IOException e)
            {
                Log.Debug("PrepareMediaRecorder", "IOException " + e.Message);
                ReleaseMediaRecorder();
                return(false);
            }

            return(true);
        }
Esempio n. 8
0
        //.............................Main Principal......................................
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            InitializeComponents();
            SetContentView(Resource.Layout.Main);
            string           date;
            string           path      = "";
            var              record    = FindViewById <Button>(Resource.Id.recordBtn);
            var              stop      = FindViewById <Button>(Resource.Id.stopBtn);
            var              play      = FindViewById <Button>(Resource.Id.playBtn);
            var              video     = FindViewById <VideoView>(Resource.Id.videoView);
            bool             isPlaying = false;
            CamcorderProfile cpHigh    = CamcorderProfile.Get(CamcorderQuality.High);

            record.Click += delegate
            {
                video.StopPlayback();
                isPlaying = true;
                recorder  = new MediaRecorder();
                recorder.SetVideoSource(VideoSource.Camera);
                recorder.SetAudioSource(AudioSource.Mic);
                //recorder.SetProfile(new CamcorderProfile());
                //recorder.SetOutputFormat(OutputFormat.Mpeg4);
                //recorder.SetVideoEncoder(VideoEncoder.H264);
                //ecorder.SetAudioEncoder(AudioEncoder.Default);
                date = DateTime.Now.ToString("yyMMddHHmmss");
                path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath + "video_" + date + ".mp4";
                recorder.SetProfile(cpHigh);
                recorder.SetPreviewDisplay(video.Holder.Surface);
                recorder.SetOutputFile(path);
                recorder.Prepare();
                StartCapturing();
                captureGPS = false;
                recorder.Start();
                record.Enabled = false;
            };
            stop.Click += delegate
            {
                if (video.IsPlaying)
                {
                    video.StopPlayback();
                    video.ClearAnimation();
                }
                if (recorder != null && isPlaying)
                {
                    recorder.Stop();
                    recorder.Release();
                    FinishCapturing();
                    ShowData();
                }
                isPlaying = false;

                InitializeComponents();
                record.Enabled = true;
            };
            play.Click += delegate
            {
                Android.Net.Uri uri = Android.Net.Uri.Parse(path);
                video.SetVideoURI(uri);
                video.Start();
            };
        }